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

时间:2021-02-19 01:47:30

I am trying to go over two arrays in a bash script, here is the declaration of the variables:s

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

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

This is the loop I have:

这是我的循环:

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

I am getting the next error when trying to execute it, looks like variable i is empty and it doesn't get any value:

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

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

1 个解决方案

#1


0  

Should be like this:

应该是这样的:

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

#1


0  

Should be like this:

应该是这样的:

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