Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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
Bash SSH中的变量问题_Bash_Variables_Loops_Ssh_While Loop - Fatal编程技术网

Bash SSH中的变量问题

Bash SSH中的变量问题,bash,variables,loops,ssh,while-loop,Bash,Variables,Loops,Ssh,While Loop,嘿,伙计们,我正在尝试运行以下代码: #!/bin/bash sudo /usr/local/bin/sshpass -p pwd ssh -o stricthostkeychecking=no -p 11022 admin@$1.test.com<<EOI i=1 while read line do location="sudo sed -n ${i}p /Users/Shared/$1.txt" number="sudo sed -n ${i}p /Users/Share

嘿,伙计们,我正在尝试运行以下代码:

#!/bin/bash

sudo /usr/local/bin/sshpass -p pwd ssh -o stricthostkeychecking=no -p 11022 admin@$1.test.com<<EOI
i=1
while read line
do
 location="sudo sed -n ${i}p /Users/Shared/$1.txt"
 number="sudo sed -n ${i}p /Users/Shared/$2n.txt"
 my_array=("${my_array[i]}" $line)
 sudo cp /Applications/TEPS\ OS\ X\ Share\ Folder/MAIN\ IMAGES\ FOLDER\ ƒ/${location}${number} /Users/Shared/FYP/$number
 sudo sips -Z 256 /Users/Shared/FYP/$number /Users/Shared/FYP/$number
 ((i++))
done </Users/Shared/$2.txt
exit
EOI
因此,如果$2应该等于2013126154714,则返回一个空字符串,如下所示

sed:/Users/Shared/n.txt:没有这样的文件或目录

正确的命令应该是

sed:/Users/Shared/2013126154714n.txt

其余的都失败了,因为2美元没有通过。
再次感谢你的帮助

ssh。。。另外,
my_array+=(“$line”)
,如果您想将单独的行(作为单个元素)添加到数组中……另外,请引用您的展开式
…此外,除非有特殊原因,否则请始终使用
-r
参数进行读取;无论何时在不使用
-r
的情况下使用它,
read
都会对您可能不想要的输入进行处理(扩展反斜杠转义序列)。我相信这会起作用,非常感谢,现在唯一的问题是$2没有被传递到循环中。有什么想法吗?我用你的建议编辑了OP,如果你想查看的话,我会用结果进行更新。再次感谢。我已经适当地更新了我的答案,以演示在SSH会话中安全地传递命令行参数。
 #!/bin/bash
        sudo /usr/local/bin/sshpass -p support ssh -o stricthostkeychecking=no -p 11022 admin@$1.noerrpole.com<<'EOI'
i=1
while read -r line
do
 location=sudo sed -n ${i}p "/Users/Shared/$1.txt"
 number=sudo sed -n ${i}p "/Users/Shared/$2n.txt"
 my_array+=( "$line" )
 sudo cp "/Applications/TEPS\ OS\ X\ Share\ Folder/MAIN\ IMAGES\ FOLDER\ ƒ/${location}${number}" "/Users/Shared/FYP/$number"
 sudo sips -Z 256 "/Users/Shared/FYP/$number" "/Users/Shared/FYP/$number"
 ((i++))
 exit
done <"/Users/Shared/$2.txt"
EOI
1:bin Photo$ bash -x thumb npco2 20131216154714
+ sudo /usr/local/bin/sshpass -p support ssh -o stricthostkeychecking=no -p 11022 admin@npco2.noerrpole.com
Pseudo-terminal will not be allocated because stdin is not a terminal.
SHPA_12-16-2013/
sed: /Users/Shared/n.txt: No such file or directory
cp: /Applications/TEPS OS X Share Folder/MAIN IMAGES FOLDER ƒ/ is a directory (not copied).
Warning: /Users/Shared/FYP/ not a valid file - skipping
Warning: /Users/Shared/FYP/ not a valid file - skipping
Error 4: no file was specified
Try 'sips --help' for help using this tool
printf -v quoted_args '%q ' "$one" "$two"
ssh user@host "bash -s - ${quoted_args}" <<<'EOI'
   ...
EOI