简体   繁体   中英

Passing a script and string over SSH

I am trying to run a perl script with a string value over SSH but I can't get it to work.

My script looks something like the following:

ssh user@linux /home/user/script.pl "value1 value2 value3"

The script will execute but for some reason the only value that gets passed to it is value1 and not the whole string value. It think this is because the double quotes are getting dropped when it transmits over SSH?.

Can anyone shed some light on this?. Cheers.

The problem is that the initial set of quotes is expanded by the shell that's being used to invoke the command, so what actually happens is:

ssh user@linux /home/user/script.pl "value1 value2 value3"

ends up as:

/home/user/script.pl value1 value2 value3

when run from the remote system.

You need to escape the quotation marks so that it doesn't get parsed locally, before being interpreted on the remote side.

To escape the quotation marks you can do:

ssh user@linux /home/user/script.pl \"value1 value2 value3\"

Usually easier to pass larger values like that as STDIN into the remote script. That way it won't get eaten or mangled by commandline parsers or shells along the way.

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