无法从 bash 脚本中 SCP 包含空格的路径吗?
我有一个 bash 脚本,它生成要执行的 SCP 命令。代码的相关部分如下所示:
echo $COPY_CMD
$COPY_CMD
我的输出如下所示:
rascher@localhost:~/Desktop/video_final$ ./xfervids.sh scp "/media/My Book/PhotosVideos/Videos/18May2008Download/SD_VIDEO/PRG001/MOV056.MOD" [email protected]:./video_working/ [email protected]'s password: "/media/My: No such file or directory Book/PhotosVideos/Videos/18May2008Download/SD_VIDEO/PRG001/MOV056.MOD": No such file or directory
但是,当我获取输出的第 2 行: scp "/media/...
时,将其复制并粘贴到终端,它工作正常。
我做错了什么?我尝试转义“My Book”(“My\ Book”
)中的空格,而是放置两个 \
字符比一个 ("My\\ Book"
) 但我无法让它表现一致,帮忙吗?
I have a bash script which generates an SCP command to execute. The relevant parts of the code look like this:
echo $COPY_CMD
$COPY_CMD
My output looks like this:
rascher@localhost:~/Desktop/video_final$ ./xfervids.sh scp "/media/My Book/PhotosVideos/Videos/18May2008Download/SD_VIDEO/PRG001/MOV056.MOD" [email protected]:./video_working/ [email protected]'s password: "/media/My: No such file or directory Book/PhotosVideos/Videos/18May2008Download/SD_VIDEO/PRG001/MOV056.MOD": No such file or directory
However, when I take line 2 of my output: scp "/media/...
, copy it, and paste it into the terminal, it works.
What am I doing wrong? I have tried escaping the space in "My Book" ("My\ Book"
), putting two \
characters rather than one ("My\\ Book"
) but I can't get this to behave consistently. Help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当路径名中存在空格时,您必须非常小心。
在上下文中,您可能需要:
这次您不需要在变量周围加上引号。
You have to be excruciatingly careful when there are spaces in the path names.
In the context, you probably need:
You don't need quotes around the variable this time.
您应该阅读此讨论,了解如何以及为何避免将命令放入变量中,并且此讨论
eval
的安全影响。您可能希望以这种方式使用变量的原因之一是记录或显示脚本所采取的操作。正如您所发现的,在能够正确引用或转义空格等时会出现问题。实现您可能想要的操作的一种方法是使用
set -x
和set +x
在脚本中的不同点打开和关闭此类输出。You should read this discussion of how and why to avoid putting commands in variables and this discussion of the security implications of
eval
.One reason you may want to use variables in this way is to log or display the actions taken by a script. Problems occur with being able to properly quote or escape spaces, etc., as you have discovered. One way to do what you may want is to use
set -x
andset +x
to turn this type of output on and off at various points in your script.