Bash:rsync 有问题 --exclude 使用带空格的变量?

Bash: Problem with rsync --exclude using variables with spaces?

我有一个 files/folders 的列表,我希望从 rsync 命令中排除,在变量 $rsyncExclude 中。例如,有些 files/folders 有空格和其他特殊字符 ($RECYCLE.BIN)。

我在这个关于 how to use rsync with options as a variable 的问题中使用了@chepner 的建议,但我仍然无法让 --exclude 正确处理我的用例。 “/System Volume Information/”等文件夹仍然复制过来。

我试过对 $rsyncExclude 使用不同的引号,例如

rsyncExclude="'.cache', ... '[$]RECYCLE.BIN/','/System Volume Information/' ... "

rsyncExclude=".cache", ... "[$]RECYCLE.BIN/","/System Volume Information/" ...

并且之间没有引号:

rsyncExclude=".cache, ... [$]RECYCLE.BIN/,/System Volume Information/ ... "

但运气不好...我做错了什么?

这是我的脚本:

#!/bin/bash

rsyncExclude=".cache/,/dev/,/proc/,/sys/,/tmp/,/mnt/,/media/,/lost+found,/Adobe/,[$]RECYCLE.BIN/,/System Volume Information/,pagefile.sys,/temp/"

From="/home/user/bash/backup/testA/"
To="/home/user/bash/backup/testA backup/"


# variables
# thanks to idea by @chepner (
options=(--dry-run --verbose --archive --human-readable --progress --stats --delete-after --itemize-changes --exclude={"${rsyncExclude}"} --delete-excluded)
# rsync
rsync "${options[@]}" "${From}" "${To}"

更新:

使用数组,似乎也不起作用:

#!/bin/bash

rsyncExclude=(".cache/", "/dev/", "/proc/", "/sys/", "/tmp/", "/mnt/", "/media/", "/lost+found", "/Adobe/", "[$]RECYCLE.BIN/", "/System Volume Information/", "pagefile.sys", "/temp/")

From="/home/user/bash/backup/testA/"
To="/home/user/bash/backup/testA backup/"


# variables
# thanks to idea by @chepner (
options=(--dry-run --verbose --archive --human-readable --progress --stats --delete-after --itemize-changes --exclude={"${rsyncExclude[@]}"} --delete-excluded)
# rsync
rsync "${options[@]}" "${From}" "${To}"

我得到以下输出:

rsync: link_stat "/dev/," failed: No such file or directory (2)
rsync: link_stat "/proc/," failed: No such file or directory (2)
rsync: link_stat "/sys/," failed: No such file or directory (2)
rsync: link_stat "/tmp/," failed: No such file or directory (2)
rsync: link_stat "/mnt/," failed: No such file or directory (2)
rsync: link_stat "/media/," failed: No such file or directory (2)
rsync: link_stat "/lost+found," failed: No such file or directory (2)
rsync: change_dir "/Adobe" failed: No such file or directory (2)
rsync: change_dir "/home/user/bash/backup//[$]RECYCLE.BIN" failed: No such file or directory (2)
rsync: change_dir "/System Volume Information" failed: No such file or directory (2)
rsync: link_stat "/home/user/bash/backup/pagefile.sys," failed: No such file or directory (2)
rsync: change_dir "/temp" failed: No such file or directory (2)
skipping directory .
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1196) [sender=3.1.2]

问题是,那些files/paths确实存在于测试目录中---即$RECYCLE.BIN、系统卷信息、.cache、pagefile.sys 等...

此脚本的答案,感谢

#!/bin/bash

rsyncExclude=(".cache/" "/dev/" "/proc/" "/sys/" "/tmp/" "/mnt/" "/media/" "/lost+found" "/Adobe/" "[$]RECYCLE.BIN/" "/System Volume Information/" "pagefile.sys" "/temp/")

From="./testA a/"
To="./testA-backup wa/"


# using variables
# thanks to idea by @chepner (
# and solution to expanding the --exclude array, thanks to @ Gordon Davisson 
options=(-vahP --dry-run --stats "${rsyncExclude[@]/#/--exclude=}")
# rsync
rsync "${options[@]}" "${From}" "${To}"

"${rsyncExclude[@]/#/--exclude=}" does three things; first, the [@] makes it expand to all of the elements of the array; then the /#/--exclude= "replaces" the beginning of each element with "--exclude=" (essentially, it prepends "--exclude=" to each element)