巴什读取退格键的行为问题 [英] Bash read backspace button behavior problem
问题描述
在bash中使用读取,pressing退格不会删除最后输入的字符,但似乎退格追加到输入缓冲区。有什么办法我可以改变它,以便删除删除输入键入的最后一个关键点?如果是的话怎么样?
下面是我使用它,如果它的任何帮助,一个简单的例子PROG:
#!/斌/庆典颜色(){#$ 1 =文本colourise $ 2 =颜色标识
printf的%s%s%S$(tput的setaf $ 2)$ 1$(tput的sgr0)
}
game_over(){#$ 1 =信息$ 2 =得分
printf的\\ n%S \\ n%S \\ n$(彩游戏结束!1),$ 1
printf的你的分数:%S \\ n$(颜色$ 2 3)
退出0
}得分= 0
明确
而真实的;做
字= $(SHUF -n1的/ usr /共享/字典/字)#random从字典中的单词
字= $ {字,,} q若要小写
LEN = $ {#字}
让超时=(3 + $ LEN)/ 2
printf的%S(时间%S):$($色字2)$(颜色$超时3)
读到这里-t $超时-n $ LEN输入#阅读输入
如果[$? -ne 0];然后
game_over你不及时回答$比分
ELIF [$输入=$词!]然后
game_over你没有输入正确的字$得分;
科幻
printf的\\ n
让得分+ = $超时
DONE
选项 -n nchars
打开终端进入原始模式,所以你最好的机会是靠的readline
(-e) [文件] :
$读-n10 -e VAR
顺便说一句,好的想法,虽然我将离开这个词的最终用户(这是一个下意识的反应,以preSS的收益的)。
When using read in bash, pressing backspace does not delete the last character entered, but appears to append a backspace to the input buffer. Is there any way I can change it so that delete removes the last key typed from the input? If so how?
Here's a short example prog I'm using it with if it's of any help:
#!/bin/bash
colour(){ #$1=text to colourise $2=colour id
printf "%s%s%s" $(tput setaf $2) "$1" $(tput sgr0)
}
game_over() { #$1=message $2=score
printf "\n%s\n%s\n" "$(colour "Game Over!" 1)" "$1"
printf "Your score: %s\n" "$(colour $2 3)"
exit 0
}
score=0
clear
while true; do
word=$(shuf -n1 /usr/share/dict/words) #random word from dictionary
word=${word,,} #to lower case
len=${#word}
let "timeout=(3+$len)/2"
printf "%s (time %s): " "$(colour $word 2)" "$(colour $timeout 3)"
read -t $timeout -n $len input #read input here
if [ $? -ne 0 ]; then
game_over "You did not answer in time" $score
elif [ "$input" != "$word" ]; then
game_over "You did not type the word correctly" $score;
fi
printf "\n"
let "score+=$timeout"
done
The option -n nchars
turns the terminal into raw mode, so your best chance is to rely on readline
(-e) [docs]:
$ read -n10 -e VAR
BTW, nice idea, although I would leave the end of the word to the user (it's a knee-jerk reaction to press return).
这篇关于巴什读取退格键的行为问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!