简体   繁体   English

带有bash for循环的SSH命令选项

[英]SSH command option with bash for loop

I want to symbolically link two arrays' elements. 我想象征性地链接两个数组的元素。 For example, array1 = (AAA BBB CCC DDD) , array2 = (001 002 003 004) , 001->AAA , 002->BBB , 003->CCC and 004->DDD . 例如, array1 = (AAA BBB CCC DDD)array2 = (001 002 003 004)array2 = (001 002 003 004) 001->AAA004->DDD 002->BBB003->CCC004->DDD

Here is the shell script I wrote, but it doesn't work, and I couldn't figure out where is wrong. 这是我写的shell脚本,但它不起作用,我无法弄清楚哪里出错了。

declare -a array1=(AAA BBB CCC DDD)
declare -a array2=(001 002 003 004)
num = ${#array1[@]}
ssh username@hostmachine 'for((i = 0 ; i < $num ; i++ )); do ln -sf ${array1[$i]} ${array2[$i]}; done' 

Can anyone give me some hints/advice? 谁能给我一些提示/建议? Thank you in advance. 先感谢您。

You should include all your bash code inside the parameter to ssh , like this: 您应该将参数中的所有bash代码包含在ssh ,如下所示:

ssh username@hostmachine 'declare -a array1=(AAA BBB CCC DDD); declare -a array2=(001 002 003 004); num = ${#array1[@]}; for((i = 0 ; i < $num ; i++ )); do ln -sf ${array1[$i]} ${array2[$i]}; done'

because otherwise the ssh bash code won't get access to your previously defined arrays, because they were defined in your computer not in the ssh one. 因为否则ssh bash代码将无法访问您之前定义的数组,因为它们是在您的计算机中定义的,而不是在ssh中定义的。

初看起来,我会说你错过了循环中的最后done

ssh username@hostmachine 'for((i = 0 ; i < $num ; i++ )); do ln -sf ${array1[$i]} ${array2[$i]}; done'

The variable substitution isn't happening insside single quotes. 变量替换不是在单引号内发生的。 Try double quotes instead: 请尝试双引号:

declare -a array1=(AAA BBB CCC DDD)
declare -a array2=(001 002 003 004)
num=${#array1[@]}
ssh username@hostmachine "for((i = 0 ; i < $num ; i++ )); do ln -sf ${array1[$i]} ${array2[$i]}; done"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM