外壳脚本响应按键 [英] shell script respond to keypress
本文介绍了外壳脚本响应按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个shell脚本,基本上说了
I have a shell script that essentially says something like
while true; do
read -r input
if ["$input" = "a"]; then
echo "hello world"
fi
done
这一切都很好,但是我只是意识到在这种情况下必须按ENTER键会带来严重的问题.我需要的是脚本在按下某个键时做出响应,而不必按回车键.
That is all well, and good, but I just realized having to hit ENTER presents a serious problem in this situation. What I need is for the script to respond when a key is pressed, without having to hit enter.
是否有一种方法可以在Shell脚本中实现此功能?
Is there a way to achieve this functionality within a shell script?
推荐答案
read -rsn1
只期待一个字母(不要等待提交)并且保持沉默(不要回信).
Expect only one letter (and don't wait for submitting) and be silent (don't write that letter back).
这篇关于外壳脚本响应按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文