无法从 bash 脚本中 SCP 包含空格的路径吗?

发布于 2024-08-16 03:00:49 字数 933 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弥枳 2024-08-23 03:00:49

当路径名中存在空格时,您必须非常小心。

在上下文中,您可能需要:

eval $COPY_CMD

这次您不需要在变量周围加上引号。

You have to be excruciatingly careful when there are spaces in the path names.

In the context, you probably need:

eval $COPY_CMD

You don't need quotes around the variable this time.

风向决定发型 2024-08-23 03:00:49

您应该阅读此讨论,了解如何以及为何避免将命令放入变量中,并且此讨论 eval 的安全影响。

您可能希望以这种方式使用变量的原因之一是记录或显示脚本所采取的操作。正如您所发现的,在能够正确引用或转义空格等时会出现问题。实现您可能想要的操作的一种方法是使用 set -xset +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 and set +x to turn this type of output on and off at various points in your script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文