Bash — Rsync variable in filename has spaces

眉间皱痕 提交于 2019-12-07 18:09:51

问题


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

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

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.

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.

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="'/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.


回答1:


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

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

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

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

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

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

And after running rsync, dir2 looks like this:

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

Can you show a specific command line that results in the errors you're seeing above? Maybe run the script with the -x flag and show exactly how localpath and remotepath are set.



来源:https://stackoverflow.com/questions/11909103/bash-rsync-variable-in-filename-has-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!