为什么这些 rsync 过滤器参数在数组中传递时在 bash 中失败?

为什么这些 rsync 过滤器参数在数组中传递时在 bash 中失败?

为什么这个 rsync 命令在我按字面给出它时有效,但在我从变量创建它时却不起作用?

以下是变量 - 首先是我作为数组传递给 rysnc 的选项:

$ echo "${options[@]}"
-av --prune-empty-dirs -f "- *.flac" -f "- *.WMA" -f "- *.wma" -f "- *.ogg" -f "- *.mp4" -f "- *.m4a" -f "- *.webm" -f "- *.wav" -f "- *.ape" -f "- *.zip" -f "- *.rar"

$ echo ${options[6]}
-f

$ echo ${options[7]}
"- *.wma"

然后是源目录,rsync 将从该目录复制文件:

$ echo "\"$dir/\""
"/media/test/Ahmad Jamal Trio/Live at the Pershing/"

以及 rsync 要将文件复制到的目标目录:

$ echo "\"$target_dir\""
"/home/test/mp3/Ahmad Jamal Trio/Live at the Pershing/"

把它们放在一起:

$ echo "${options[@]}" "\"$dir/\"" "\"$target_dir\""
-av --prune-empty-dirs -f "- *.flac" -f "- *.WMA" -f "- *.wma" -f "- *.ogg" -f "- *.mp4" -f "- *.m4a" -f "- *.webm" -f "- *.wav" -f "- *.ape" -f "- *.zip" -f "- *.rar" "/media/test/Ahmad Jamal Trio/Live at the Pershing//" "/home/test/mp3/Ahmad Jamal Trio/Live at the Pershing/"

这一切看起来都应该如此。事实上,如果你按字面意思给出命令,它确实有效,如下所示:

$ rsync -av --prune-empty-dirs -f "- *.flac" -f "- *.WMA" -f "- *.wma" -f "- *.ogg" -f "- *.mp4" -f "- *.m4a" -f "- *.webm" -f "- *.wav" -f "- *.ape" -f "- *.zip" -f "- *.rar" "/media/test/Ahmad Jamal Trio/Live at the Pershing/" "/home/test/mp3/Ahmad Jamal Trio/Live at the Pershing/"
./
Ahmad Jamal Trio - Live at the Pershing - 01 - But Not for Me.mp3
Ahmad Jamal Trio - Live at the Pershing - 02 - Surrey With The Fringe On Top.mp3
Ahmad Jamal Trio - Live at the Pershing - 03 - Moonlight In Vermont.mp3
Ahmad Jamal Trio - Live at the Pershing - 04 - Music, Music, Music.mp3
Ahmad Jamal Trio - Live at the Pershing - 05 - No Greater Love.mp3
Ahmad Jamal Trio - Live at the Pershing - 06 - Poinciana.mp3
Ahmad Jamal Trio - Live at the Pershing - 07 - Wood'yn You.mp3
Ahmad Jamal Trio - Live at the Pershing - 08 - What's New.mp3
AlbumArtSmall.jpg
AlbumArtLarge.jpg
Folder.jpg

sent 43,194,376 bytes  received 285 bytes  28,796,440.67 bytes/sec
total size is 43,182,454  speedup is 1.00

但当我使用变量作为参数调用 rsync 时,它失败了:

$ rsync "${options[@]}" "\"$dir/\"" "\"$target_dir\""
Unknown filter rule: `"- *.flac"'
rsync error: syntax or usage error (code 1) at exclude.c(902) [client=3.1.2]

答案1

部分rsync过滤器以及源目录和目标目录用附加转义引号引起来。删除转义引号,它应该可以工作:

options=(
  -av --prune-empty-dirs 
  -f "- *.flac" 
  -f "- *.WMA" 
  -f "- *.wma" 
  -f "- *.ogg" 
  -f "- *.mp4" 
  -f "- *.m4a" 
  -f "- *.webm" 
  -f "- *.wav" 
  -f "- *.ape" 
  -f "- *.zip" 
  -f "- *.rar"
)
dir="/media/test/Ahmad Jamal Trio/Live at the Pershing"
target_dir="/home/test/mp3/Ahmad Jamal Trio/Live at the Pershing"
rsync "${options[@]}" "$dir/" "$target_dir"

dir我从和变量中删除了尾随斜杠target_dir/已经添加到调用$dirrsync

相关内容