根据三胞胎,塞缪尔的建议,我在这里编辑了我的问题,代替了前一个令人困惑的问题,希望新一轮谈判会变得更加不稳定。
问题可能是在向终端仿真器发出查询/答复序列之前,如何在stdin上耗尽所有类型。
下面的演示在脚本中模拟了命令循环,读取了用户“命令”,执行此操作(此处为睡眠),然后“希望”查询/回复终端仿真器(此处要求输入术语ID,但是您可以想象终端仿真器会接受的任何类型的查询),因此在查询/回复之前,我们必须先清空任何类型的查询。在这里,我尝试读取直到为空。
这是脚本
typeset -i i=0 n=30
while((i<n))
do
read -p "cmd: " c # Read user command
sleep 1 # emulate user command that takes time
echo "cmd '$c' done"
while read -s -n 1 -t .1 a # drain type ahead
do [ "$a" = "" ] && break;
done
read -s -dc -p $'\e[0c' r # emit a terminal query and read reply.
[ "$r" != "$gr" ] && echo "Got corrupted reply '$r' '$gr'"
((i++))
done
运行时,键入一个1字母的命令“ a”,然后 每秒钟一次,慢慢地做。
使用bash BASH_VERSION ='4.4.19(1)-release'和两个mate-terminal或xterm我都知道了
每秒键入一次
cmd: a
cmdcmd 'a' done
cmdcmd: a
cmdcmd 'a' done
cmdcmd: a
cmdcmd 'a' done
cmdcmd:
我会尽快输入'a'。
cmdcmd: a
cmda
cmda
cmda
cmda
cmdacmd 'a' done
cmdGot corrupted reply 'a
cmda
cmda
cmda
cmdmd:
cmda
cmda
cmda
cmda
cmdcmd '' done
cmdGot corrupted reply 'a
cmda
问题是,如何消耗输入以消除前面不需要的类型。
答案 0 :(得分:0)
read -d ':' _
会读取输入,直到第一个冒号为止。
while read -d :
do
printf 'Preread: Got "%s"\n' "$REPLY"
read -r a
printf 'In loop: Got "%s"\n' "$a"
done <<\_
bad
bad
: good
bad
bad
: good
bad
bad
: good
bad
bad
: good
_
输出:
Preread: Got "bad
bad
"
In loop: Got "good"
Preread: Got "bad
bad
"
In loop: Got "good"
Preread: Got "bad
bad
"
In loop: Got "good"
Preread: Got "bad
bad
"
In loop: Got "good"