Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何使用用户输入作为历史记录_Bash_History - Fatal编程技术网

Bash 如何使用用户输入作为历史记录

Bash 如何使用用户输入作为历史记录,bash,history,Bash,History,我正在编写一个脚本,需要大量用户输入,然后根据输入创建一个文件。有些输入是相当重复的,因此我想启用向上箭头功能,以包括已经输入的字符串。我很难找到如何做到这一点 我试过了 set -o history 但这只给出了实际运行的命令,而不是收到的输入。我也知道 "\e[A" 是向上箭头命令,但这是我所知道的。测试脚本如下所示: #!/bin/bash set -o history read -e date read -e date2 read -e date3 echo $date $d

我正在编写一个脚本,需要大量用户输入,然后根据输入创建一个文件。有些输入是相当重复的,因此我想启用向上箭头功能,以包括已经输入的字符串。我很难找到如何做到这一点

我试过了

set -o history
但这只给出了实际运行的命令,而不是收到的输入。我也知道

"\e[A" 
是向上箭头命令,但这是我所知道的。测试脚本如下所示:

#!/bin/bash

set -o history

read -e date
read -e date2
read -e date3

echo $date $date2 $date3

输入
$date
后,我希望能够向上箭头并获取
$date
的内容,以用于
$date2
。有什么想法吗?

你可以试着玩一下这个小片段:

#!/bin/bash

HISTFILE=myhistoryfile
history -r
for i in {1..3}; do
   read -ep "Enter date $i: " d
   history -s "$d"
done
history -w
echo "Thanks for using this script. The history is:"
history
我们定义要用作历史文件的文件:
myhistoryfile
,并使用
history-r
将其加载到内存中。然后我们进入循环,在每个用户输入变量
d
后执行

history -s "$d"
history -w
将输入
d
附加到当前历史记录。在循环结束时,我们执行

history -s "$d"
history -w
将其写入历史文件


尝试不同的组合:有/没有
history-s
history-w
,以了解它们真正的功能。还有

这起作用了。我把那本手册拉了起来,但是我没有看到
-s
命令。