Bash -- 文件名中的 Rsync 变量有空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11909103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:58:53  来源:igfitidea点击:

Bash -- Rsync variable in filename has spaces

bashvariablesdirectoryrsyncspaces

提问by KroniK907

Been searching for hours and still cant figure this out :(

已经搜索了几个小时,但仍然无法弄清楚:(

Anyway, im creating a script that will automatically rsync about 40 different directories to 40 other directories on another server. if you want to see the entire script you can view it here: http://pastebin.com/Nt3KKvx9

无论如何,我正在创建一个脚本,该脚本将自动将大约 40 个不同的目录同步到另一台服务器上的 40 个其他目录。如果您想查看整个脚本,可以在此处查看:http: //pastebin.com/Nt3KKvx9

But the important bit is the for loop where i run rsync

但重要的一点是我运行 rsync 的 for 循环

for ((i=0; i<${#websys[@]}; i++))
do
  localpath="/nusdata/staff/NUS/NUS/Systems/${kiskasys[$i]}"
  remotepath="/home/www/html/nusalaska.com/html/systems/${websys[$i]}"
  rsync -rlptnvz -s "$localpath" -e "ssh -p 50014" "nusak@webserver:$remotepath/"
done 

The problem is that the array "kiskasys" has many directory names that have spaces in them (Example: '101 greenbrook'). I have tried making the array variables have single quotes around them, double quotes around them, escaped spaces like '\ ', and combinations of all three. I have also tried putting the $localpath in quotes, not in quotes, etc. etc.

问题是数组“kiskasys”有许多包含空格的目录名称(例如:'101 greenbrook')。我曾尝试使数组变量在它们周围有单引号,在它们周围有双引号,像 '\ ' 这样的转义空格,以及所有三者的组合。我也试过将 $localpath 放在引号中,而不是放在引号中等等。

I guess im just confused on how the -s (--protect-args) deals with the spaces and how I can get it to work in my situation.

我想我只是对 -s (--protect-args) 如何处理空格以及如何让它在我的情况下工作感到困惑。

The error output always looks something like the following:

错误输出始终如下所示:

rsync: change_dir "/nusdata/staff/NUS/NUS/101" failed: No such file or directory (2)

or

或者

rsync: change_dir "/nusdata/staff/NUS/NUS/'101 greenbrook'" failed: No such file or directory (2)

Any help is appreciated!

任何帮助表示赞赏!

Thanks

谢谢

Found My Problem

发现我的问题

In copying my code from my script to this page I accidently copied it wrong... however in copying it wrong, what i posted above was perfectly good code that works fine haha.

在将我的代码从我的脚本复制到此页面时,我不小心复制了错误...但是在复制错误时,我上面发布的是非常好的代码,可以正常工作哈哈。

so what i posted above was the solution to my own problem.

所以我上面发布的是我自己问题的解决方案。

The original had single quotes in the localpath variable like so:

原来的 localpath 变量中有单引号,如下所示:

localpath="'/nusdata/staff/NUS/NUS/Systems/${kiskasys[$i]}'"

and the single quotes was the problem. And for everyone's benefit here is an example output

单引号是问题所在。为了大家的利益,这里是一个示例输出

echo ${kiskasys[1]}

#output would look like this:

101 greenbrook

Basically there are no special escape characters etc.

基本上没有特殊的转义字符等。

回答by larsks

For what it's worth, I'm not able to replicate your problem. If I set localpathand remotepathto a directory with spaces:

对于它的价值,我无法复制您的问题。如果我将localpath和设置remotepath为带有空格的目录:

localpath="/home/lars/tmp/so/dir1/a directory"
remotepath="/home/lars/tmp/so/dir2/a directory"

And then run your rsynccommand (modified slightly for my environment):

然后运行您的rsync命令(针对我的环境稍作修改):

rsync -rlptvz -s "$localpath/" -e "ssh" "localhost:$remotepath/"

It Just Works. Here's the content of dir1:

它只是有效。以下是内容dir1

dir1/a directory/file1
dir1/a directory/file3
dir1/a directory/file2

And after running rsync, dir2looks like this:

运行 rsync 后,dir2如下所示:

dir2/a directory/file1
dir2/a directory/file3
dir2/a directory/file2

Can you show a specificcommand line that results in the errors you're seeing above? Maybe run the script with the -xflag and show exactly how localpathand remotepathare set.

您能否显示导致您在上面看到的错误的特定命令行?也许运行带有-x标志的脚本并准确显示如何设置localpathremotepath设置。