これらの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

おすすめ記事