如何启用向上/向下箭头键来显示previous投入使用`read`什么时候? [英] How to enable the up/down arrow keys to show previous inputs when using `read`?
问题描述
我在一个无限循环等待用户输入(使用'读'),并希望有命令历史,就是能够显示已经输入了previous输入,使用向上和向下箭头钥匙,而不是让^ [[A和^ [[B。这可能吗?
感谢@ l0b0你的答案。这让我在正确的方向。用它玩了一段时间后,我已经意识到我还需要以下两个特点,但我没有设法让他们尚未:
-
如果我preSS和添加一些东西到previous命令我想有保存在历史上整个事情,而不仅仅是增加。示例
$ ./up_and_down结果
输入命令:您好结果
<大骨节病> ENTER 骨节病>结果
输入命令:结果
<大骨节病>最多骨节病>结果
输入命令:你好你结果
<大骨节病> ENTER 骨节病>结果
输入命令:结果
<大骨节病>最多骨节病>结果
输入命令:你结果
(而不是你好你) -
如果我不能继续下去了,因为我在历史数组的结尾,我不想将光标移动到previous线,而不是我希望它保持固定的。
这是我迄今(up_and_down):
#!的/ usr / bin中/ env的庆典
设置-o nounset -o errexit -o pipefailread_history(){
本地字符
本地字符串
当地ESC = $'\\ E'
当地起来= $'\\ E [A'
当地下跌= $'\\ E [B'
当地clear_line = $'\\ r \\ E [K'
当地历史=()
当地-i history_index = 0 #在同一时间读取一个字符
而IFS =读-p中输入命令:-n1 -s字符;做
如果[$字符==$ ESC]];然后
#获取转义序列的其余部分(3个字符总数)
而阅读-N2 -s休息;做
字符+ =$休息
打破
DONE
科幻 如果[$字符==$向上和放大器;&安培; $ history_index&GT; 0]];然后
history_index + = - 1
回声-ne $ clear_line $ {历史[$ history_index]}
ELIF [$字符==美元降至&放大器;&安培; $ history_index&LT; $(($ {#历史[@]} - 1))]];然后
history_index + = 1
回声-ne $ clear_line $ {历史[$ history_index]}
ELIF [[-z$字符]];然后用户#pressed ENTER
回声
历史+ =($字符串)
字符串=
history_index = $ {#历史[@]}
其他
回声-n$字符
字符串+ =$字符
科幻
DONE
}
read_history
使用 -e
选项到读$ C两个解决方案$ C>命令与内建结合
历史
命令:
#1版
而IFS =读-r -e -d $的'\\ n'-p'输入&GT; '剖面做
回声$线
历史-s$线
DONE#2版
而IFS =读-r -e -d $的'\\ n'-p'输入&GT; '剖面做
回声$线
回声$行&GT;&GT; 〜/ .bash_history的
历史-n
DONE
I'm waiting for user input (using 'read') in an infinite loop and would like to have command history, that is being able to show previous inputs that were already entered, using the up and down arrow keys, instead of getting ^[[A and ^[[B. Is this possible?
Thanks to @l0b0 for your answer. It got me on the right direction. After playing with it for some time I've realized I also need the following two features, but I haven't managed to get them yet:
If I press up and add something to the previous command I would like to have the whole thing saved in the history, not just the addition. Example
$ ./up_and_down
Enter command: hello
ENTER
Enter command:
Up
Enter command: hello you
ENTER
Enter command:
Up
Enter command: you
(instead of "hello you")If I can't keep going up because I'm at the end of the history array, I don't want the cursor to move to the previous line, instead I want it to stay fixed.
This is what I have so far (up_and_down):
#!/usr/bin/env bash
set -o nounset -o errexit -o pipefail
read_history() {
local char
local string
local esc=$'\e'
local up=$'\e[A'
local down=$'\e[B'
local clear_line=$'\r\e[K'
local history=()
local -i history_index=0
# Read one character at a time
while IFS="" read -p "Enter command:" -n1 -s char ; do
if [[ "$char" == "$esc" ]]; then
# Get the rest of the escape sequence (3 characters total)
while read -n2 -s rest ; do
char+="$rest"
break
done
fi
if [[ "$char" == "$up" && $history_index > 0 ]] ; then
history_index+=-1
echo -ne $clear_line${history[$history_index]}
elif [[ "$char" == "$down" && $history_index < $((${#history[@]} - 1)) ]] ; then
history_index+=1
echo -ne $clear_line${history[$history_index]}
elif [[ -z "$char" ]]; then # user pressed ENTER
echo
history+=( "$string" )
string=
history_index=${#history[@]}
else
echo -n "$char"
string+="$char"
fi
done
}
read_history
Two solutions using the -e
option to the read
command combined with the builtin history
command:
# version 1
while IFS="" read -r -e -d $'\n' -p 'input> ' line; do
echo "$line"
history -s "$line"
done
# version 2
while IFS="" read -r -e -d $'\n' -p 'input> ' line; do
echo "$line"
echo "$line" >> ~/.bash_history
history -n
done
这篇关于如何启用向上/向下箭头键来显示previous投入使用`read`什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!