[英]How to use user input as history
我正在编写一个需要大量用户输入的脚本,然后根据该输入创建一个文件。 有些输入是相当重复的,因此我想启用向上箭头功能以包含已经放入的字符串。我很难找到如何执行此操作。
我努力了
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"
将输入d
附加到当前历史记录。 在循环结束时,我们执行
history -w
实际上将其写入历史文件。
尝试不同的组合:有/无history -s
或history -w
以了解他们真正做了什么。 并阅读手册 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.