Shell 正在向 rsync 失败的变量添加单引号

发布于 2025-01-01 11:20:25 字数 1424 浏览 2 评论 0原文

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 rsyncs. 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 技术交流群。

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

发布评论

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

评论(1

吻泪 2025-01-08 11:20:25

我假设没有一个名称包含空格,因为您使用空格作为分隔符,那么这样的事情怎么样:

while read line
do
    line=($line) # split line on its spaces
    rsync -av -e 'ssh -p 2222' [email protected]:${line[0]} ${line[1]}
done < file_list

这一次读取您的 file_list 一行,在空格处将其分割,然后使用第一个和第二个“单词”作为参数。

正如 Johnsyweb 所指出的,您可能意味着该目录实际上确实有一个空格,并且您希望将其传递给 rsync。在这种情况下,我会说:

while read line
do
    rsync -av -e 'ssh -p 2222' "[email protected]:${line// /\ }"
done < file_list

这应该会导致所有空格都被转义,我认为这可能是您之前遗漏的。

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:

while read line
do
    line=($line) # split line on its spaces
    rsync -av -e 'ssh -p 2222' [email protected]:${line[0]} ${line[1]}
done < file_list

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:

while read line
do
    rsync -av -e 'ssh -p 2222' "[email protected]:${line// /\ }"
done < file_list

That should cause all spaces to be escaped, which I think might be what you were missing before.

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