[英]Bash script answer interactive prompts
我想創建一個腳本,用於生成一些報告,但要做到這一點,我必須回答幾個問題,這些問題由我運行的其他腳本提出。
答案可能是,不是,對於一些我需要 select 1,2 或 3 的問題,我需要“按 [Enter] 繼續:”......所以我的問題是如何以最好的方式做到這一點?
這是我現在的腳本(它不起作用它在第二個問題上停止,我需要回答為 Y)答案的順序是:是/是/是/ENTER/2/3/1/否/否/是
#!/bin/bash
while read LINE ; do
ssh $LINE <<"EOF"
echo "$HOSTNAME"
printf '\Y \nY \nY \n \n2 \n3 \n1 \nN \nN \nY' |/tmp/script.sh
done
EOF
done < file1 >> /tmp/result.log
好的,現在我在需要 select 2/3/1 的階段遇到問題,它沒有回答 2 請參閱日志中的示例...
Choose the Veritas enterprise product family:
1@) NetBackup Family
2@) Storage Foundation and HA Solutions / InfoScale Solutions
Choose your option: [1-2,q] (1)@ Invalid input. Re-enter.
Choose your option: [1-2,q] (1)@
您需要將換行符明確寫入腳本。
printf 'Y\nY\nY\n\n2\n3\n1\nn\nn\nY\n' | /tmp/script.sh
這假設Y
對應於“是”, n
對應於“否”。 您還可以讓printf
在 arguments 上循環,以減少\n
的重復。
printf '%s\n' Y Y Y "" 2 3 1 n n Y | /tmp/script.sh
空字符串對應於在沒有任何其他輸入的情況下按 Enter。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.