scripting rsync ssh port issue

Matt McCutchen matt at mattmccutchen.net
Fri Apr 4 16:20:01 GMT 2008


Paul has addressed the errant dollar signs, the misspelled $DST, and two
possible workarounds for the quoting issue, but I would like to explain
the quoting issue a bit further.

On Fri, 2008-04-04 at 11:23 -0400, Sterling Windmill wrote:
> RSYNC_OPTIONS="-aH --delete --numeric-ids -e \'ssh -p 2292\'"
> RSYNC="ionice -c3 rsync $RSYNC_OPTIONS"
>  
> $SOURCE=/some/dir
> $DEST=root at 10.2.10.1:/some/dir
>  
> $RSYNC --progress  "$SOURCE" "$DST"

> Missing trailing-' in remote-shell command.
> rsync error: syntax or usage error (code 1) at main.c(364)
> [sender=3.0.0]
>  
> If I paste the command directly into a command line, the command works
> properly.

First, get rid of the backslashes in $RSYNC_OPTIONS because they are
being taken literally; single quotes need no escaping inside double
quotes.

Second, when bash expands a variable like $RSYNC, it takes quotes in the
variable's value literally; they do not protect spaces in the expanded
value from word splitting.  Thus, bash is splitting your -e value into
three pieces, the first and last of which contain unmatched single
quotes, hence the rsync error.

You could fix this by using "eval" instead of word-splitting expansion,
but that would be fragile with respect to shell metacharacters in the
arguments to be passed to rsync.  Alternatively...

> I've also tried taking the advice mentioned here and using an array
> instead of a variable, but to no avail:
>  
> http://wooledge.org:8000/BashFAQ/050

Arrays are generally the best solution, but it takes some care to use
them correctly.  Here's your script rewritten to use arrays:

---

RSYNC_OPTIONS=(-aH --delete --numeric-ids -e 'ssh -p 2292')
RSYNC=(ionice -c3 rsync "${RSYNC_OPTIONS[@]}")

SOURCE=/some/dir
DEST=root at 10.2.10.1:/some/dir

: "${RSYNC[@]}" --progress "$SOURCE" "$DEST"

---

Matt
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://lists.samba.org/archive/rsync/attachments/20080404/585e174b/attachment.bin


More information about the rsync mailing list