[英]Whats wrong with this list of servers in my bash script?
我正在編寫一個簡單的bash腳本( checkServs.sh
),它將ssh到服務器列表中並對它們執行運行狀況檢查。
我不斷在以下行中收到錯誤:
SERVERS=(blah1.example.com blah2.example.com blah3.example.com blah4.example.com)
錯誤是:
checkServs.sh: 3: checkServs.sh: Syntax error: "(" unexpected
我已經檢查了在線示例,這似乎是正確的,不是嗎? 提前致謝!
我不知道語法錯誤,但這應該工作:
SERVERS="blah1.example.com blah2.example.com blah3.example.com blah4.example.com"
for server in $SERVERS
do
echo $server
done
編輯 :正如Jonathan Leffler在評論中所指出的,也許你沒有用bash
運行腳本。 其他shell(例如dash
)可能無法識別數組語法。 如果是這種情況,你可以這樣做:
SERVERS=(blah1.example.com blah2.example.com blah3.example.com blah4.example.com)
for i in $(seq 0 3)
do
echo ${SERVERS[$i]}
done
但是如果你只是想循環遍歷名稱並運行SSH命令(即如果有一個數組不提供有用的功能),第一種方法就更簡單了。
您的遠程服務器在執行命令時可能會調用不同的shell。 嘗試將bash -c添加到您的參數:
ssh user@server bash -c "<your commands>"
要么:
ssh user@server bash < yourscript.sh ## None in yourscript.sh must read input though.
一個左括號開始一個子shell,這在一個等號的右邊不是正確的。 它需要一個字符串表達式,而不是一個命令。
引號用於將字符串表達式保持在一起。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.