SSH 中的变量问题 [英] Variable issues in SSH

查看:27
本文介绍了SSH 中的变量问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hey guys I'm trying to run this code:

#!/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

基本上它读取一个文本文件,该文件给出了某些图像的位置,并将创建这些图像的缩略图,稍后可以下载.问题是我需要 $i 的值来设置 $location 和 $number 的值,但是当我在 while 循环中设置变量时,变量没有设置.我尝试使用单引号、双引号在本地和全局设置它,通过 sshpass 传递,导出它 - 这可以作为测试使用,但 $i 当然是未知的 - 尝试放置方括号、花括号、圆括号、转义 $、在这一点上我已经用尽了我的想法,这可能是非常简单的事情,但我可以使用一双新的眼睛,非常感谢任何帮助!

basically it reads a text file which gives the location of certain images, and will create a thumbnail of those images, which can be downloaded later. The problem is that I need the value of $i to set the values of $location and $number, but when I set the variable within the while loop the variables are not set. I've tried setting it locally and globally with single quotes, double quotes, passing through with the sshpass, exporting it -This works as a test but $i is of course unknown- tried placing brackets, curly braces, parentheses, escaping $, at this point I have exhausted my ideas, it's probably something incredibly simple, but I could use a fresh pair of eyes, any help is greatly appreciated!

感谢 Charles Duffy 帮助我清理它,这就是我现在所拥有的:

Thanks to Charles Duffy for helping me clean it up so this is what I have now:

 #!/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

但是现在 $2 没有被传递到循环中,这就是我得到的

But now $2 isn't getting passed through to the loop here's what I get back

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

所以 $2 应该等于 20131216154714 它会返回一个像这样的空字符串

So where $2 should equal 20131216154714 it's returning an empty string like this

sed:/Users/Shared/n.txt: 没有那个文件或目录

正确的命令是

sed:/Users/Shared/20131216154714n.txt

其余的都失败了,因为 $2 没有通过.再次感谢您的帮助!

The rest is just failing because $2 isn't passed. Again thanks for the help!

推荐答案

ssh ... <<EOI 在启动 ssh 之前在本地进行扩展.使用 ssh ... <<'EOI' 在远端做扩展.

ssh ... <<EOI does expansion on the local end, before starting ssh. Use ssh ... <<'EOI' to do expansions on the remote end.

如果您想传递参数,请使用 printf '%q ' 引用它们,以便它们在远程转义时完好无损:

If you want to pass arguments, use printf '%q ' to quote them so they survive remote unescaping intact:

printf -v quoted_args '%q ' "$one" "$two"
ssh user@host "bash -s - ${quoted_args}" <<<'EOI'
   ...
EOI

这篇关于SSH 中的变量问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆