--exclude patterns

Matt McCutchen matt at mattmccutchen.net
Tue Jan 29 19:25:31 GMT 2008


On Mon, 2008-01-28 at 15:00 -0800, Randy Dunlap wrote:
> Sorry about the missing information.  Here's the script,
> which still isn't working.
> 
> Any help/suggestions would be wonderful.
> ---
> ~Randy
> 
> 
> #! /bin/sh
> # rsync /test/runs/ to $server:~rdunlap/pub/kerneltest/reports/runs/
> 
> rsync_one() {
>         srcdir=$1
>         dest=$2
>         destdir=$3
> 
>         rsync $EXCLUDE -avz -e ssh --progress --delete $srcdir $dest:$destdir/
> }
> 
> #1
> EXCLUDE="--exclude='/?' --exclude='/??' --exclude='/???' --exclude='/1???'"
> rsync_one /test/runs/ rdunlap at server pub/kerneltest/reports/runs
> 
> # end.

The shell does not strip the single quotes because they are inside
double quotes.  Thus, rsync is getting filter patterns containing single
quotes, which would obviously prevent the filters from matching.

You can just remove the single quotes, but then the shell will try to
expand the wildcards when it expands $EXCLUDE.  Depending on your
working directory, the expansion may or may not cause a problem.  (IMHO,
this shell behavior is weird; I would prefer if the shell expanded only
unquoted literal wildcards and *not* those in the expansions of unquoted
variable references.)

I think the best way to store multiple arguments safely is to use a
shell array.  This lets you make a quoted reference to "all the
arguments in the array" without them getting mangled any further.  (But
shell arrays may require a relatively modern shell, and they can't be
passed from a process to its children through the environment.  See
"Arrays" in the bash man page for more information.)  Here is your
script rewritten to use a shell array:

-----
#! /bin/sh
# rsync /test/runs/ to $server:~rdunlap/pub/kerneltest/reports/runs/

set -x

rsync_one() {
        srcdir=$1
        dest=$2
        destdir=$3

        rsync "${EXCLUDE[@]}" -avz -e ssh --progress --delete $srcdir $dest:$destdir/
}

#1
EXCLUDE=(--exclude='/?' --exclude='/??' --exclude='/???' --exclude='/1???')
rsync_one /test/runs/ rdunlap at server pub/kerneltest/reports/runs

# end.
-----

Matt



More information about the rsync mailing list