BASH:如何询问用户输入并将其存储在未来?

时间:2022-12-28 15:45:00

I want to store a user message in a bash program, and then display the message the next time the user runs the script.

我想在bash程序中存储用户消息,然后在用户下次运行脚本时显示该消息。

One way I thought this might work is if I export the message to an environmental variable, but I cannot get it to work.

我认为这可能有用的一种方法是,如果我将消息导出到环境变量,但我无法使其工作。

Here is what I have so far, but it is not working:

这是我到目前为止所做的,但它不起作用:

echo "Last message was: $KEEPTHISMESSAGE"
echo "Type the new message that you want to enter, followed by [ENTER]:"
read KEEPTHISMESSAGE
export KEEPTHISMESSAGE

What am I doing wrong? If there is a better way to do this, please let me know. Maybe keep a file that keeps a history of these message and gets the most recent?

我究竟做错了什么?如果有更好的方法,请告诉我。也许保留一个文件,保存这些消息的历史记录,并获取最新的?

2 个解决方案

#1


7  

You cannot use EXPORT this way. It only exports to processes started from within that invocation of the script. You must store the message in a file on the filesystem and load it in the next time your user executes the script. Very simply:

您不能以这种方式使用EXPORT。它只导出到从脚本调用内部开始的进程。您必须将消息存储在文件系统上的文件中,并在用户下次执行脚本时加载它。非常简单:

echo "Last message was: $(cat message.txt)"
echo "Type the new message that you want to enter, followed by [ENTER]:"
read KEEPTHISMESSAGE
echo $KEEPTHISMESSAGE > message.txt

You'll have to work out what happens the first time (when message.txt doesn't exist), and issues with relative/absolute paths to message.txt if you're running the script in a different directory.

您将不得不弄清楚第一次发生的事情(当message.txt不存在时),并且如果您在另一个目录中运行脚本,则会发出message.txt的相对/绝对路径。

#2


2  

Scripts can only directly export variables to their sub-processes. They can't export to parent processes.

脚本只能直接将变量导出到其子流程。它们无法导出到父进程。

You can alter a parents environment by invoking your script like this:

您可以通过调用以下脚本来更改父项环境:

$ . /path/to/your_script.sh

Here your script should have an export statement to export the variable and must not have an exit statement.

在这里,您的脚本应该有一个export语句来导出变量,并且不能有exit语句。

#1


7  

You cannot use EXPORT this way. It only exports to processes started from within that invocation of the script. You must store the message in a file on the filesystem and load it in the next time your user executes the script. Very simply:

您不能以这种方式使用EXPORT。它只导出到从脚本调用内部开始的进程。您必须将消息存储在文件系统上的文件中,并在用户下次执行脚本时加载它。非常简单:

echo "Last message was: $(cat message.txt)"
echo "Type the new message that you want to enter, followed by [ENTER]:"
read KEEPTHISMESSAGE
echo $KEEPTHISMESSAGE > message.txt

You'll have to work out what happens the first time (when message.txt doesn't exist), and issues with relative/absolute paths to message.txt if you're running the script in a different directory.

您将不得不弄清楚第一次发生的事情(当message.txt不存在时),并且如果您在另一个目录中运行脚本,则会发出message.txt的相对/绝对路径。

#2


2  

Scripts can only directly export variables to their sub-processes. They can't export to parent processes.

脚本只能直接将变量导出到其子流程。它们无法导出到父进程。

You can alter a parents environment by invoking your script like this:

您可以通过调用以下脚本来更改父项环境:

$ . /path/to/your_script.sh

Here your script should have an export statement to export the variable and must not have an exit statement.

在这里,您的脚本应该有一个export语句来导出变量,并且不能有exit语句。