在 BusyBox shell 中重新命名帶有空格的文件

在 BusyBox shell 中重新命名帶有空格的文件

我正在嘗試透過 SSH 重命名 Synology Diskstation 上的一些檔案。可用的 shell 是 BusyBox 內建 shell:

BusyBox v1.16.1 (2013-04-16 20:13:10 CST) built-in shell (ash)

當我嘗試在來源檔案名稱或目標檔案名稱中使用空格字元時,移動命令總是會產生兩個錯誤。轉義空格字元或引用檔案名稱似乎沒有效果。

在目標中重新命名帶有空格字元的檔案的範例:

/volumeUSB1/usbshare/directory $ touch test
/volumeUSB1/usbshare/directory $ ls
test
/volumeUSB1/usbshare/directory $ mv test 'te st'
mv: can't rename 'test': No such file or directory
mv: can't rename 'te': No such file or directory
/volumeUSB1/usbshare/directory $ mv test te\ st
mv: can't rename 'test': No such file or directory
mv: can't rename 'te': No such file or directory

在來源中使用空格字元重新命名檔案會產生類似的結果:

/volumeUSB1/usbshare/directory $ touch 'te st'
/volumeUSB1/usbshare/directory $ ls
te st
/volumeUSB1/usbshare/directory $ mv 'te st' test
mv: can't rename 'te': No such file or directory
mv: can't rename 'st': No such file or directory
/volumeUSB1/usbshare/directory $ mv te\ st test
mv: can't rename 'te': No such file or directory
mv: can't rename 'st': No such file or directory

type mv返回mv is /bin/mv。該file命令在我的機器上不可用。cat /bin/mv透露這是一個以呼叫結尾的小腳本/bin/busybox mv $@

我的錯誤在哪裡?

答案1

正如您所添加的,該命令是最後一行的mv腳本:/bin/mv

/bin/busybox mv $@

該行缺少引號$@

/bin/busybox mv "$@"

$@表示提供給腳本的參數清單。引用此變數具有特殊意義,即擴展時,每個參數都將單獨引用。這至少對於和 是有效的bashdashbusybox

這樣,mv當參數包含帶引號的空格時,該指令也應該會起作用。

相關內容