模拟击键 [英] Simulate key press in bash
问题描述
我有一个问题,我有一个非常简单的脚本,它可以循环启动另一个二进制文件,看起来像这样:
I have a question, Im having very simple script that starts anpther binary file in loop it looka like this:
for (( i=0; \\$i <= 5; i++ )) ; do
test.sh
done
现在的问题是,每次执行后,test.sh都会询问我是否要覆盖日志,例如您是否要覆盖日志?[是/否]"
Now problem is that after each execution test.sh asks me if I want to to override log something Like "Do you want to override log? [Y/n]"
在该提示出现之后,脚本暂停并且迭代停止,直到我手动按Y,然后继续直到出现另一个提示为止.
After that prompt appears scripts pauses and iteration is stopped until I manually press Y and that it continues until another prompt appears.
要使过程自动化,我可以模拟按下"Y"按钮吗?
To automate process can I simulate pressing "Y" button?
推荐答案
我相信,如果您的test.sh
脚本未将其标准输入用于其他目的,则使用yes
可能就足够了:yes
将产生一个无限大的默认情况下y
的行流,或者您将其作为参数传递的任何其他字符串.每次test.sh
检查用户输入时,它应该消耗该输入的一行并继续执行其操作.
I believe using yes
might be enough if your test.sh
script doesn't use its standard input for other purposes : yes
will produce an infinite stream of lines of y
by default, or any other string you pass it as a parameter. Each time the test.sh
checks for user input, it should consume a line of that input and carry on with its actions.
使用yes Y
,您可以为test.sh
脚本提供更多的Y
脚本:
Using yes Y
, you could provide your test.sh
script with more Y
than it will ever need :
yes Y | test.sh
要在循环中使用它,您最好将其通过管道传递到循环的stdin而不是test.sh
调用:
To use it with your loop, you might as well pipe it to the loop's stdin rather than to the test.sh
invocation :
yes Y | for (( i=0; i <= 5; i++ )) ; do
test.sh
done
这篇关于模拟击键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!