简体   繁体   中英

Passing multiple variables from local bash to remote bash script without gobbling

I'm having trouble sending multiple variables to a remote bash script without gobbling occurring.

For the sake of this question the variable $timestamp contains 12-12-15 19:45:21

ssh user@serverip "/usr/path/to/script.sh http://www.web.com/$1 http://web.com/$2 $timestamp";

I am sending 3 variables to script.sh

Two URLs with an amended file name in the form of a variable on the end and then my $timestamp variable

But on myscript.sh, when I try to insert $timestamp into a mysql database it only see's the first part of the date before the white space :

12-12-15   

So my quotes around the command aren't preventing gobbling. Do I need to quote each variable separately?

ssh user@serverip "/usr/path/to/script.sh http://www.web.com/$1 http://web.com/$2 $timestamp";

This is equivalent to this locally calling

/usr/path/to/script.sh http://www.web.com/$1 http://web.com/$2 $timestamp

Try to quote each individual argument passed

ssh user@serverip "/usr/path/to/script.sh 'http://www.web.com/$1' 'http://web.com/$2' '$timestamp'";

You can also print each argument in the script to see what's being passed... eg echo $1 , etc.

您可以尝试类似

ssh localhost "printf \"%s %s %s\n\" a b \"last parameter\""

You need to escape the values for the remote host. The correct way of doing this is with printf %q :

ssh user@serverip "/usr/path/to/script.sh \
    $(printf "%q " "http://www.web.com/$1" "http://web.com/$2" "$timestamp")"

This works for all variable values. Wrapping them in single quotes would instead result in syntax error and command injection when the variables themselves contain single quotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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