如何在shell脚本中使用向上箭头键来获取上一个命令

时间:2021-08-21 19:24:22

I am trying to write my console with some basic functionalities. Here is what I am doing.

我正在尝试编写具有一些基本功能的控制台。这就是我在做的事情。

function help()
{
        echo "add(a,b,...)"
}

function add()
{
arg=$(echo $1 | cut -d"(" -f2)
sum=0
for number in `echo ${arg} | sed -e 's/[,) ]\+/\n/g'` ; do
        sum=$(($sum + $number))
done
echo $sum
}

while true
do

echo -n "mycon@avi>>"
read command
opt=$(echo "$command" | cut -d"(" -f1)
case $opt in
"exit"|"q")
        exit
;;
"help")
        help
;;
"add")
        add $command
;;
esac
done

I am saving this file as mycon when I run this script ./mycon

我在运行此脚本时将此文件保存为mycon ./mycon

mycon@avi>>add(2,3)
5
mycon@avi>>

Now in this moment when I am pressing up arrow key, I want to get the above add(2,3) command. what is the way to do this ??

现在,在我按下箭头键的那一刻,我想得到上面的add(2,3)命令。这样做的方法是什么?

Thank You

1 个解决方案

#1


Bash-only solution:

  1. Change read command to read -e command so that bash will enable the readline library.

    将read命令更改为read -e命令,以便bash启用readline库。

  2. Add the command history -s "$command" to include the line read into the history.

    添加命令history -s“$ command”以包括读入历史记录的行。

Note that read command will delete trailing whitespace from the typed command before assigning the line to command, unless you invoke it with IFS set to the empty string. Also, read will normally treat backslashes as escape characters, which is usually undesirable; you can suppress that behaviour with the -r flag. Finally, you can get read to print the prompt, which will work better with readline, using the -p option. So your final sequence might look like this:

请注意,在将行指定给命令之前,read命令将从typed命令中删除尾随空格,除非您在IFS设置为空字符串的情况下调用它。另外,read通常将反斜杠视为转义字符,这通常是不合需要的;您可以使用-r标志来抑制该行为。最后,您可以阅读打印提示,使用-p选项可以更好地使用readline。所以你的最终序列可能如下所示:

while IFS= read -e -p "mycon@avi>> " command; do
  history -s "$command"
  # ... process the command
done

(Using the read command as the condition in the while statement causes the while loop to terminate if the user enters an EOF character.)

(如果用户输入EOF字符,则使用read命令作为while语句中的条件会导致while循环终止。)

For more information on read and history, use the built-in bash help command (help read / help history)

有关读取和历史记录的更多信息,请使用内置bash help命令(帮助读取/帮助历史记录)

#1


Bash-only solution:

  1. Change read command to read -e command so that bash will enable the readline library.

    将read命令更改为read -e命令,以便bash启用readline库。

  2. Add the command history -s "$command" to include the line read into the history.

    添加命令history -s“$ command”以包括读入历史记录的行。

Note that read command will delete trailing whitespace from the typed command before assigning the line to command, unless you invoke it with IFS set to the empty string. Also, read will normally treat backslashes as escape characters, which is usually undesirable; you can suppress that behaviour with the -r flag. Finally, you can get read to print the prompt, which will work better with readline, using the -p option. So your final sequence might look like this:

请注意,在将行指定给命令之前,read命令将从typed命令中删除尾随空格,除非您在IFS设置为空字符串的情况下调用它。另外,read通常将反斜杠视为转义字符,这通常是不合需要的;您可以使用-r标志来抑制该行为。最后,您可以阅读打印提示,使用-p选项可以更好地使用readline。所以你的最终序列可能如下所示:

while IFS= read -e -p "mycon@avi>> " command; do
  history -s "$command"
  # ... process the command
done

(Using the read command as the condition in the while statement causes the while loop to terminate if the user enters an EOF character.)

(如果用户输入EOF字符,则使用read命令作为while语句中的条件会导致while循环终止。)

For more information on read and history, use the built-in bash help command (help read / help history)

有关读取和历史记录的更多信息,请使用内置bash help命令(帮助读取/帮助历史记录)