在bash脚本中嵌套for循环内的变量无法识别

时间:2018-01-22 09:48:55

标签: bash loops variables for-loop nested

我试图在bash脚本中检查两个数组,这里是变量的声明:s

declare -a servers=("server1" "server2")
declare -a services=("service1" "service2")

这是我的循环:

for j in "${servers[@]}";do
            'ssh -q -o StrictHostKeyChecking=no user@'$j';
            '"for i in "${services[@]}";do echo "$i status in $j"; 
"'/etc/init.d/service-'$i' status'"; done"''
done

我在尝试执行它时遇到下一个错误,看起来变量我是空的并且它没有得到任何值:

for i in service1' 'service2;do echo ' status in 'server1; /etc/init.d/service- status; done'
for i in service1: command not found

1 个答案:

答案 0 :(得分:0)

应该是这样的:

declare -a servers=("server1" "server2")
declare -a services=("service1" "service2")

for server in ${servers[@]}; do
    for service in ${services[@]}; do
        ssh user@$server /etc/init.d/service-$service status
    done
done