Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 带有bash for循环的SSH命令选项_Arrays_Linux_Bash_For Loop_Ssh - Fatal编程技术网

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

Arrays 带有bash for循环的SSH命令选项,arrays,linux,bash,for-loop,ssh,Arrays,Linux,Bash,For Loop,Ssh,我想象征性地链接两个数组的元素。例如,array1=(AAA BBB CCC DDD),array2=(001 002 003),001->AAA,002->BBB,003->CCC和004->DDD 这是我写的shell脚本,但它不起作用,我也不知道哪里出错了 declare -a array1=(AAA BBB CCC DDD) declare -a array2=(001 002 003 004) num = ${#array1[@]} ssh username@hostmachine '

我想象征性地链接两个数组的元素。例如,
array1=(AAA BBB CCC DDD)
array2=(001 002 003)
001->AAA
002->BBB
003->CCC
004->DDD

这是我写的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' 
declare-a数组1=(AAA BBB CCC DDD)
声明-a数组2=(001 002 003 004)
num=${#array1[@]}
sshusername@hostmachine(i=0;i<$num;i++);do ln-sf${array1[$i]}${array2[$i]};完成

有人能给我一些提示/建议吗?提前感谢您。

第一眼,我想说您的循环中缺少了最后一个
完成的

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

变量替换不会发生在单引号内。请尝试使用双引号:

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"
declare-a数组1=(AAA BBB CCC DDD)
声明-a数组2=(001 002 003 004)
num=${#array1[@]}
sshusername@hostmachine“对于((i=0;i<$num;i++);do ln-sf${array1[$i]}${array2[$i]};完成”

您应该在
ssh
的参数中包含所有bash代码,如下所示:

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'
sshusername@hostmachine“申报-a阵列1=(AAA BBB CCC DDD);声明-a数组2=(001 002 003 004);num=${#array1[@]};对于((i=0;i<$num;i++));do ln-sf${array1[$i]}${array2[$i]};完成

否则,ssh bash代码将无法访问以前定义的数组,因为它们是在您的计算机中定义的,而不是在ssh计算机中定义的。

同时发布您收到的错误消息。哎呀,没有键入它。但是,我的脚本在那一行中有“完成”,但它仍然不能正常工作。没有明显的错误,只是没有在远程机器上得到任何链接。@user1846268这很奇怪。我在不使用ssh的情况下尝试了您的代码片段,它的效果与预期一样。ssh是否询问您的密码?有任何输出吗?是的,没有ssh,它可以正常工作。我确实输入了正确的密码,但它在“主机”上无法正常工作。顺便说一句,只有每个数组中的第一个元素在“主机”上链接,即:001->AAA。没有其他的。是的,我也试过了,只有每个数组中的第一个元素链接到“主机”上,即:001->AAA。没有其他人。似乎“$i”没有被正确迭代。是的,我越想它,就越觉得需要重写解决方案。主机无法访问您的环境,因此您需要在ssh命令中提供它(它不知道什么是array1、array2或num)answer@Nelson这似乎有效:myArray=“一件事另一件事”;ssh myhost“用于${myArray[@]};do echo\$myValue;done;”中的myValue;但这不是:myArray=“onething-anotherthing”;ssh Thufig-0105“用于((i=0;i