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
但是當我使用 vars 作為 args 呼叫 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]
部分
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
andtarget_dir
變數中刪除了尾部斜杠,/
已經$dir
在rsync
呼叫中添加了。