嗨,我正在尝试实施一个在5秒倒计时后发生的事件,除非按下一个键.我一直在使用这段代码,但是如果我按下回车键或空格键就会失败.在输入或空间被检测为“”的意义上它失败了.
echo "Phoning home..." key="" read -r -s -n 1 -t 5 -p "Press any key to abort in the next 5 seconds." key echo if [ "$key" = "" ] # No Keypress detected,phone home. then python /home/myuser/bin/phonehome.py else echo "Aborting." fi
看完这篇文章后,
Bash: Check if enter was pressed
我放弃了,发布在这里.我觉得必须有比我试图实施的更好的方法.
阅读手册说:
The return code for
read
is zero,unless end-of-file is encountered
or read times out.
在您的情况下,当用户在允许的时间内点击任何键时,您希望中止,否则继续.
#!/bin/bash if read -r -s -n 1 -t 5 -p "TEST:" key #key in a sense has no use at all then echo "aborted" else echo "continued" fi
参考:
Read Manual
注意:引文的重点是我的.