如何防止用户不在特定位置进行输入? (重击)

如何防止用户不在特定位置进行输入? (重击)

我编写一个基本脚本来测试某些东西。假设我想显示下面的行(没有充分的理由):

#!/bin/bash

while true; do
    echo -e " _____________________________________"
    echo -e "|_____________________________________|"
    echo -e "|_____________________________________|"
    echo -e "|_____________________________________|"
    echo -e "|_____________________________________|"
    echo -e "|_____________________________________|"

    read -p "Type a letter: " letter
done

如果我正常使用该程序,请给出输入a输出:

user@DESKTOP:/mnt/c/Users/user/Documents$ ./script.sh
 _____________________________________
|_____________________________________|
|_____________________________________|
|_____________________________________|
|_____________________________________|
|_____________________________________|
Type a letter: a
 _____________________________________
|_____________________________________|
|_____________________________________|
|_____________________________________|
|_____________________________________|
|_____________________________________|
Type a letter:

但如果我非常快地向终端输入输入,我就会像这样打破艺术:

user@DESKTOP:/mnt/c/Users/user/Documents$ ./script.sh
a
 _____________________________________
|_____________________________________|
|_____________________________________|
|_____________________________________|
|_____________________________________|
|_____________________________________|
Type a letter:  _____________________________________
|_____________________________________|
|_____________________________________|
|_____________________________________|
|_____________________________________|
|_____________________________________|
Type a letter:

\n我之前尝试过------,有效。但在某些情况下,我不应该使用\n

我该如何防止这种情况?谢谢你!

答案1

#!/bin/bash

while true; do
    stty -echo
    echo -e " _____________________________________"
    echo -e "|_____________________________________|"
    echo -e "|_____________________________________|"
    echo -e "|_____________________________________|"
    echo -e "|_____________________________________|"
    echo -e "|_____________________________________|"
    read -t .1 -n 100 buf
    stty echo
    read -e -i $buf -p "Type a letter: " letter
done

相关内容