我的bash脚本中的这个服务器列表有什么问题?

时间:2013-08-15 03:00:10

标签: bash shell syntax-error

我正在编写一个简单的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

我已经查过在线示例,这看起来是正确的,不是吗?提前谢谢!

3 个答案:

答案 0 :(得分:4)

我不知道语法错误,但这应该有效:

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命令(即如果有一个数组不提供有用的功能),第一种方法就更简单了。

答案 1 :(得分:0)

执行命令时,您的远程服务器可能会调用另一个shell。尝试将bash -c添加到您的参数中:

ssh user@server bash -c "<your commands>"

或者:

ssh user@server bash < yourscript.sh  ## None in yourscript.sh must read input though.

答案 2 :(得分:-1)

一个左括号开始一个子shell,这在等号的右边不是正确的。它需要一个字符串表达式,而不是一个命令。

引号用于将字符串表达式保持在一起。