In a Bash script:
I need to pass an exclude option with a wildcard to rsync. The twist is that sometimes the wildcard needs to be omitted.
Without a wildcard, this is easy:
Code:
if some_condition; then EXCLUDE="--exclude='/data/'"; else EXCLUDE=''; fi
rsync ... ${EXCLUDE} ...
If some_condition is true, rsync gets --exclude='/data/', otherwise there's no exclude option.
When it comes to wildcards, it's more complicated. You need to turn off globbing as follows otherwise Bash expands it.
Code:
if some_condition; then EXCLUDE="--exclude='/*'"; else EXCLUDE=''; fi
set -o noglob
rsync ... ${EXCLUDE} ...
set +o noglob
I would have thought that this would work. However, when I run this in real life, rsync always acts as if EXCLUDE='' even when some_condition is true.
I have checked that EXCLUDE has been correctly set.
If I hard-code --exclude='/*' instead of using ${EXCLUDE}, then rsync works correctly.
What am I doing wrong?
Bookmarks