views:

47

answers:

3

I normally work these things out reasonably quickly, but this one is a headache.

I have a shell script that loads some info about where to connect to and how from a text file, this is done using the read command. This works. It stores the arguments to be sent onto RSync in a variable call $rsyncargs.

I want to use the RSync arg -e which is used to pass details onto ssh, in my case a port number as so:

rsync -avz -e "ssh -p 222" dir/ [email protected]:dir/

the line in the text file looks like so:

-e "ssh -p 222"

the line of bash should be something like:

rsync -avz $rsyncargs $src $dst

this all works, except that the quotes mess up and I end up with

sending incremental file list
rsync: link_stat "/dir/to/shell/script/222"" failed: No such file or directory (2)

So it's trying to parse the contents of the quotes at the wrong stage. I've tried a list of different escaping mechanisms (like backslashes in the text file and using single quotes instead of double quotes in several places etc) but it all seems to end with similarly catastrophic (though not always identical) error messages.

any suggestions would be greatly appreciated (either to solve the original problem, or to get the ssh port changed in a less annoying way)

A: 

you can always set the port in your ssh config:

>cat ~/.ssh/config
Host shortName
   Hostname [actual url or ip]
   User otherUserName
   Port 22
   ForwardAgent yes
   ForwardX11 yes
second
That will certainly work as an interim measure, and may well end up being the selected solution.
Thingomy
+1  A: 

I think they need to look like this:

rsyncargs="ssh -p 222"

rsync -avz -e $rsyncargs $src $dst

I'm assuming what you have is:

rsyncargs='-e "ssh -p 222"'

which is likely to be what's causing the errors because the double quotes and the option switch aren't part of the argument.

Dennis Williamson
I specifically don't want to do this (although I will if I have to, at a loss of some functionality) because I want to be able to call any random rsync args in this way. For example: --modify-window=3
Thingomy
@Thingomy: Then you probably need to put your arguments in an array: `rsyncargs=('-e' 'ssh -p 222'); rsync -avz ${rsyncargs[@]} $src $dst`. And see [BashFAQ/050](http://mywiki.wooledge.org/BashFAQ/050).
Dennis Williamson
A: 

I agree with @Dennis Williamson and the answer is that you probably need less quoting not more, i.e.:

less_quote_rsyncargs=$(echo $rsyncargs)
rsync -avz $less_quote_rsyncargs $src $dst
Chen Levy