There is an input argument with a list of files (files with wildcards or folders) like this one:
list="file1 dir1 **.data **.source"
Now each element of this list must be prefixed with --filter=+
, turning it into a list of arguments of rsync
command, looking something like this:
args='--filter=="+ file1" --filter="+ dir1" --filter="+ **.data" --filter="+ **.source"'
So, when passing $args to rsync $args
it should receive each argument correctly where the space in the "+ " must be just a character of the argument and not a separator of two arguments.
How to do that in shell script (bash) using only built-in shell commands? Notice that rsync
command must receive each argument correctly.
ONE UNSUCCESSFUL TRY:
#!/bin/bash
set -f
list="file1 dir1 **.data **.source"
ct=0
arr=""
for i in $list; do
ct=$(( ct + 1 ))
arr[$ct]="--filter='+ $i'"
done
args=${arr[*]}
set -x
echo args:$args
rsync $args srcdir destdir
set +x
When it's run (supposing that the folder srcdir
exists in the current directory), it shows:
$ ./test
+ echo args: '--filter='\''+' 'file1'\''' '--filter='\''+' 'dir1'\''' '--filter='\''+' '**.data'\''' '--filter='\''+' '**.source'\'''
args: --filter='+ file1' --filter='+ dir1' --filter='+ **.data' --filter='+ **.source'
+ rsync '--filter='\''+' 'file1'\''' '--filter='\''+' 'dir1'\''' '--filter='\''+' '**.data'\''' '--filter='\''+' '**.source'\''' a b
Unknown filter rule: `'+'
rsync error: syntax or usage error (code 1) at exclude.c(904) [client=3.1.1]
+ set +x
Look that although echo
shows each argument correctly:
args: --filter='+ file1' --filter='+ dir1' --filter='+ **.data' --filter='+ **.source'
The rsync
command looks like not understanding each argument correctly, as the "+" finished the argument and the space wasn't part of the argument but a separator, corrupting all arguments.