加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Bash读取退格按钮行为问题

发布时间:2020-12-16 01:19:49 所属栏目:安全 来源:网络整理
导读:在bash中使用read时,按退格键不会删除输入的最后一个字符,但似乎会在输入缓冲区中附加一个退格键.有什么方法可以更改它,以便删除从输入中键入的最后一个键?如果是这样的话? 这是一个简短的示例编程我正在使用它,如果有任何帮助: #!/bin/bashcolour(){ #$1
在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%sn%sn" "$(colour "Game Over!" 1)" "$1"
        printf "Your score: %sn" "$(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

顺便说一句,好主意,虽然我会把这个词的结尾留给用户(这是对按回车的一个下意识的反应).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读