开发者

Quoatation escaping -- rsync from bash

I normall开发者_如何学Cy 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/ bob@server.com: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)


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


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.


I had a problem like this, I found that this works, no quotes in the external shell option and escape the spaces it needs:

rsync -azvxSe ssh\ -p$PORT $src $SERVER:/$dst

I also have no space between the -p and $PORT, which I think would completely solve the OP's issue.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜