本文介绍了需要在shell脚本中模拟一系列键盘按键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我是 shell 脚本的初学者,对脚本中的模拟键盘按键不太了解.

Since i am beginner in shell scripting and don't know much about the simulating keyboard keys in scripting.

在我需要运行的一个脚本中,bq init 在此之后它会询问一系列问题,包括在按下 后按下 yes/no 等键盘事件yes 再次询问 yes or no 然后它要求输入像 1 或 2 这样的数字.

In one of my script i need to run, bq init after this it asks series of questions which involves keyboard events like pressing yes/no after pressing yes again it asks yes or no then it asks to enter the number like 1 or 2.

我可以通过运行来管理按下第一个键盘按键事件echo "y" |big init 但对于以后的问题/键,我一无所知.任何人都可以帮助解决这个问题....

I can manage pressing 1st keyboard key event just by running echo "y" | big init but for later questions/keys i am clueless.can anyone help with this....

推荐答案

有很多方法可以做到这一点.这种方式可能看起来很直观(heredoc"):

There are lots of ways to do this. This way may seem intuitive (a "heredoc"):

yourScript << EOF
bq init
yes
no
maybe
1
2
EOF

或者这可能会吸引您(复合声明"):

Or this may appeal to you (a "compound statement"):

{ echo "yes"; echo "no"; echo "maybe"; echo "1"; } | yourScript

或者,同样的逐行拼写:

Or, the same spelled out line by line:

{ 
   echo "yes"
   echo "no"
   echo "maybe"
   echo "1"
} | yourScript

或者像这样使用printf:

printf "%s\n" "yes" "no" "maybe" "1" | yourScript

这篇关于需要在shell脚本中模拟一系列键盘按键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 09:49