2016-12-16 53 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