我有一个bash脚本:
#!/bin/bash
OriginFilePath="/home/lv2eof/.config/google-chrome/Profile 1/"
OriginFileName="Bookmarks"
OriginFilePathAndName="$OriginFilePath""$OriginFileName"
DestinationFilePath="/home/Config/Browser/Bookmarks/ScriptSaved/Chrome/Profile 1/"
DestinationFileName=$(date +%Y%m%d-%H%M%S-Bookmarks)
DestinationFilePathAndName="$DestinationFilePath""$DestinationFileName"
echo cp \"$OriginFilePathAndName\" \"$DestinationFilePathAndName\"
cp \"$OriginFilePathAndName\" \"$DestinationFilePathAndName\"当我从命令行执行它时,我得到以下输出:
[~/]
lv2eof@PERU $$ csbp1
cp "/home/lv2eof/.config/google-chrome/Profile 1/Bookmarks" "/home/Config/Browser/Bookmarks/ScriptSaved/Chrome/Profile 1/20211207-001444-Bookmarks"
cp: target '1/20211207-001444-Bookmarks"' is not a directory
[~/]
lv2eof@PERU $$ 所以我得到了一个错误,文件没有被复制。然而,如果我在命令行中发出命令:
[~/]
lv2eof@PERU $$ cp "/home/lv2eof/.config/google-chrome/Profile 1/Bookmarks" "/home/Config/Browser/Bookmarks/ScriptSaved/Chrome/Profile 1/20211207-001444-Bookmarks"
[~/]
lv2eof@PERU $$ 如您所见,一切正常,文件被复制。命令在bash脚本内部和外部的工作方式不应该相同吗?我做错了什么?
发布于 2021-12-07 04:32:16
这也许很难注意到,但是消息给了你两个提示:
cp: target '1/20211207-001444-Bookmarks"' is not a directory
| |
| +-- Notice quote
+-- Space in target换句话说,1/20211207-001444-Bookmarks"不是一个目录。那它为什么要这么说?
在您的脚本中有:
cp \"$OriginFilePathAndName\" \"$DestinationFilePathAndName\"通过escaping引号,您可以说引号是参数的一部分。或者:威胁引用为文字文本。它们与变量的值连接在一起。
应:
cp "$OriginFilePathAndName" "$DestinationFilePathAndName"简而言之:引用变量来告诉bash这应该是线程化的一个参数。
从您的问题中,cp的实际参数变为4,而不是2:
"/home/lv2eof/.config/google-chrome/Profile1/Bookmarks""/home/Config/Browser/Bookmarks/ScriptSaved/Chrome/Profile1/20211207-001444-Bookmarks"换句话说,复制1、2和3到4。
https://unix.stackexchange.com/questions/680434
复制相似问题