Shell 正在向 rsync 失败的变量添加单引号
Shell (/bin/bash
) 向 for
循环中从文件读取的变量添加单引号,并导致 rsync
失败。
我有一个 shell 脚本执行一堆 rsync。输入文件如下所示: /home/account/this
子目录
for 循环如下所示:
IFS=$'\n'
for SOURCE_DEST in `cat file_list`
do
rsync -av -e 'ssh -p 2222' [email protected]:$SOURCE_DEST
done
如果没有 IFS=$'\n'
,默认情况下会导致 $SOURCE_DEST
仅包含: /home/account/this
而不是 /home/account/this 子目录
。
但是,当我使用 IFS=$'\n'
运行此脚本时,它会在 $SOURCE_DEST
两边加上单引号,例如:'[email protected]:/home/account/this subdirectory'
这会导致 rsync 失败并执行奇怪的操作。
示例:
rsync --delete-after -av -e 'ssh -p 2222' '[email protected]:/home/account/this subdirectory'
失败了,我需要的是不带单引号的:
rsync --delete-after -av -e 'ssh -p 2222' [email protected]:/home/account/this subdirectory
Shell (/bin/bash
) is adding single quotes to variables read in from a file in a for
loop and causing rsync
to fail.
I have an shell script doing a bunch of rsync
s. The input file looks like this:/home/account/this
subdirectory
The for loops looks like this:
IFS=
Without IFS=$'\n'
, the default causes $SOURCE_DEST
to only contain:
/home/account/this
instead of /home/account/this subdirectory
.
However, when I run this script with the IFS=$'\n'
, it puts single quotes around the $SOURCE_DEST
such as: '[email protected]:/home/account/this subdirectory'
which causes rsync to fail and do strange things.
Example:
rsync --delete-after -av -e 'ssh -p 2222' '[email protected]:/home/account/this subdirectory'
which fails, and what I need is this without the single quotes:
rsync --delete-after -av -e 'ssh -p 2222' [email protected]:/home/account/this subdirectory
\n'
for SOURCE_DEST in `cat file_list`
do
rsync -av -e 'ssh -p 2222' [email protected]:$SOURCE_DEST
done
Without IFS=$'\n'
, the default causes $SOURCE_DEST
to only contain:/home/account/this
instead of /home/account/this subdirectory
.
However, when I run this script with the IFS=$'\n'
, it puts single quotes around the $SOURCE_DEST
such as: '[email protected]:/home/account/this subdirectory'
which causes rsync to fail and do strange things.
Example:
which fails, and what I need is this without the single quotes:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设没有一个名称包含空格,因为您使用空格作为分隔符,那么这样的事情怎么样:
这一次读取您的 file_list 一行,在空格处将其分割,然后使用第一个和第二个“单词”作为参数。
正如 Johnsyweb 所指出的,您可能意味着该目录实际上确实有一个空格,并且您希望将其传递给 rsync。在这种情况下,我会说:
这应该会导致所有空格都被转义,我认为这可能是您之前遗漏的。
I'm going to assume none of the names contain spaces, since you're using a space as a delimiter, so how about something like this:
This reads your file_list a line at a time, splits it at the space, then uses the first and second "word" it had as the args.
As pointed out by Johnsyweb, you might have meant that the directory actually did have a space and you wanted to pass that to
rsync
. In that case, I'd say:That should cause all spaces to be escaped, which I think might be what you were missing before.