2016-12-16 50 views
0

我有一个问题按键,林具有启动anpther二进制文件中循环非常简单的脚本它looka这样的:模拟在bash

for ((i=0; \\$i <= 5; i++)) ; do 
test.sh 
done 

现在的问题是,经过每次执行test.sh问我,如果我想覆盖日志类似于“你想覆盖日志吗?”[Y/n]“

之后,出现脚本暂停和迭代停止,直到我手动按Y并且它继续,直到出现另一个提示。

要自动化过程,我可以模拟按“Y”按钮吗?

+0

'expect'完全符合您的需求。 – Aserre

+1

也许'yes'就够了。 '是Y'会产生一个无限的'Y'流,你可以将它输入'test.sh'的stdin。 – Aaron

+0

小心发表一个答案,@Aaron? –

回答

2

我相信使用yes如果您test.sh脚本不使用它的标准输入用于其他目的可能就足够了:yes将生产线的无限流y默认情况下,或任何其他字符串,你传递它作为参数。每次test.sh检查用户输入时,它应该消耗该输入的一行并继续执行其操作。

使用yes Y,你可以提供你的test.sh脚本更Y比它永远都需要:

yes Y | test.sh 

要与你的循环使用它,你还不如它管循环的标准输入,而不是到test.sh调用:

yes Y | for ((i=0; i <= 5; i++)) ; do 
test.sh 
done 
2

如下面的代码片段的东西应该工作:

for ((i=0; i <= 5; i++)) 
#heredoc. the '-' is needed to take tabulations into acount (for readability sake) 
#we begin our expect bloc 
do /bin/usr/expect <<-EOD 
    #process we monitor 
    spawn test.sh 
    #when the monitored process displays the string "[Y/n]" ... 
    expect "[Y/n]" 
    #... we send it the string "y" followed by the enter key ("\r") 
    send "y\r" 
#we exit our expect block 
EOD 
done