我有以下bash腳本deploy.sh
:爲什麼這個while循環不循環?
#!/usr/bin/env bash
# Some useful resources:
# while read ip user pass; do : http://unix.stackexchange.com/questions/92664/how-to-deploy-programs-on-multiple-machines
# -o StrictHostKeyChecking=no: http://askubuntu.com/questions/180860/regarding-host-key-verification-failed
# -T: http://stackoverflow.com/questions/21659637/how-to-fix-sudo-no-tty-present-and-no-askpass-program-specified-error
# echo $pass |: http://stackoverflow.com/questions/11955298/use-sudo-with-password-as-parameter
while read ip user pass; do
echo $ip
sshpass -p "$pass" ssh [email protected]$ip -o StrictHostKeyChecking=no -T "
echo 'yo'
"
echo 'done'
done < servers.txt
servers.txt
包含:
53.12.45.74 my_username my_password
54.12.45.74 my_username my_password
57.12.45.74 my_username my_password
從我的理解,應該while read ip user pass; do […] done < servers.txt
遍歷所有三行servers.txt
。
然而,當我嘗試運行它,它只能執行一次迭代:
[email protected]:~$ bash deploy.sh
53.12.45.74
yo
done
[email protected]:~$
爲什麼?
如果循環很簡單:
while read ip user pass; do
echo $ip
done < servers.txt
它並執行所有三個迭代:
[email protected]:~$ bash deploy.sh
53.12.45.74
54.12.45.74
57.12.45.74
[email protected]:~$
這是一個完整的腳本還是真實劇本的代表?如果它是一個代表,你可能忽略了while循環中的break語句。 – alvits
@alvits完成腳本,我只是改變了IP,用戶名和密碼。 –
看起來'sshpass'正在控制'stdin'或可能替換它並導致'while'循環丟失來自重定向的'stdin'的輸入。嘗試在'(sshpass -p「$ pass」ssh $ user @ $ ip -o StrictHostKeyChecking = no -T「)子shell中運行'sshpass' echo $ pass | sudo -S sudo /etc/init.d/ganglia -monitor restart 「)'並查看問題是否得到解決。 – alvits