在 bash 中使用 read 时,按退格键不会删除最后输入的字符,但会在输入缓冲区中追加一个退格键。有什么方法可以更改它,以便删除删除从输入中键入的最后一个键?如果是这样怎么办?
这是一个简短的示例程序,我正在使用它,如果它有任何帮助的话:
#!/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
选项-n nchars
将终端转变为原始模式,所以你最好的机会是依靠readline
(-e) [docs]:
$ read -n10 -e VAR
顺便说一句,好主意,尽管我会把单词的结尾留给用户(这是按下时的下意识反应)return).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)