Variable issues in SSH

2020-05-02 03:56发布

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

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!

EDIT: 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

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

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

sed: /Users/Shared/n.txt: No such file or directory

The correct command would be

sed: /Users/Shared/20131216154714n.txt

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

1条回答
等我变得足够好
2楼-- · 2020-05-02 04:25

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

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
查看更多
登录 后发表回答