Visit Our UNIX and Linux User Community


Spaces in a rsync variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Spaces in a rsync variable
# 1  
Old 10-18-2009
Question Spaces in a rsync variable

The following code doesn't work because of the space in "My Documents".
Is there a way to set the "sources" variable and then use it in rsync?
I tried escape space (\ ) and quotes, but nothing worked.

Code:
#!/bin/sh
sources="'/home/wolf'  '/media/sda1/Documents and Settings/Administrator/My Documents'"
destination="/media/USBdrive/backups/backup1"

rsync -rltxn "$sources" "$destination"

What is the proper way to preserve spaces in this situation?

Thank you.
# 2  
Old 10-18-2009
Have you tried:
Code:
#!/bin/sh
sources="/home/wolf  '/media/sda1/Documents and Settings/Administrator/My Documents'"
destination=/media/USBdrive/backups/backup1
rsync -rltxn $sources $destination

If you use "" around $sources then rsync will see it as one argument, whereas it should be two separate arguments..

You could also try:
Code:
sources="/home/wolf  /media/sda1/Documents\ and\ Settings/Administrator/My\ Documents"

# 3  
Old 10-18-2009
Hi Scrutinizer,
I tried both, and both got this error:
Code:
rsync: link_stat "/media/USBdrive/backups/backup1" failed: No such file or directory (2)

# 4  
Old 10-18-2009
What does
Code:
ls -ld /media/USBdrive/backups/backup1

say?
# 5  
Old 10-18-2009
Scrutinizer,

Thank you for taking a look at this. Here is the code for backup_now_test2:
Code:
#!/bin/sh
sources="/home/wolf/test  '/media/sda1/Documents and Settings/Administrator/My Documents'"
destination="/media/USBdrive/backups/backup1"

echo "\$sources causes an error:"
rsync -rltxn $sources $destination
echo "end rsync\n"

echo "sources work fine when they are a string literal:"
rsync -rltxn /home/wolf/test  '/media/sda1/Documents and Settings/Administrator/My Documents' $destination
echo "end rsync\n"

Here is the output of backup_now_test2 and "ls -ld /media/USBdrive/backups/backup1" and "ls -ld /media/USBdrive/backups":
Code:
wolf-PC:~/PC_maintenance/rsync> sudo ./backup_now_test2
$sources causes an error:
rsync: change_dir "/home/wolf/PC_maintenance/rsync/'/home/wolf" failed: No such file or directory (2)
rsync: change_dir "/home/wolf/PC_maintenance/rsync/'/media/sda1" failed: No such file or directory (2)
rsync: link_stat "/home/wolf/PC_maintenance/rsync/and" failed: No such file or directory (2)
rsync: change_dir "/home/wolf/PC_maintenance/rsync/Settings/Administrator" failed: No such file or directory (2)
rsync: link_stat "/home/wolf/PC_maintenance/rsync/Documents'" failed: No such file or directory (2)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1058) [sender=3.0.5]
end rsync

sources work fine when they are a string literal:
end rsync

wolf-PC:~/PC_maintenance/rsync> ls -ld /media/USBdrive/backups/backup1
ls: cannot access /media/USBdrive/backups/backup1: No such file or directory
wolf-PC:~/PC_maintenance/rsync> ls -ld /media/USBdrive/backups
drwx------ 3 wolf root 16384 2009-10-12 23:24 /media/USBdrive/backups
wolf-PC:~/PC_maintenance/rsync>

When it works, rsync creates the destination backup1.

Last edited by wolfv; 10-18-2009 at 12:37 PM..
# 6  
Old 10-18-2009
Hi Wolfv,

The problem lies in the fact that either you put quotes around the variables, but then rsync interprets them a single path. Or you do not put quotes around them but then the spaces can not be escaped. I can see two work-arounds:

1. Put the source paths in separate variables
Code:
sources="/home/wolf/test"
sources2="/media/sda1/Documents and Settings/Administrator/My Documents"
destination="/media/USBdrive/backups/backup1"

rsync -rltxnv $sources "$sources2" $destination

You can put as many directories in $sources as you like but you would have to use a separate variable for each path that contains spaces.


2. Use eval.
Code:
sources="/home/wolf/test '/media/sda1/Documents and Settings/Administrator/My Documents'"
destination="/media/USBdrive/backups/backup1"

eval rsync -rltxnv $sources $destination


Previous Thread | Next Thread
Test Your Knowledge in Computers #813
Difficulty: Medium
The CSS RGBA alpha parameter defines the opacity as a number between 1.0 (fully transparent) and 0.0 (fully opaque).
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Add spaces to variable

Hi, I'm passing a variable to a scrpit which can be 1 to 3 characters long. How can I force it to be three character long and add spaces to it? The passed variable is stored in $1 and I would like to be stored in NewName I tried without success NewName=$(printf "%*s 3 $1) So if... (2 Replies)
Discussion started by: f_o_555
2 Replies

2. Shell Programming and Scripting

Trimming spaces from a variable

Hi guys, when I take substring of a particular data using this command var=substr($0,11,10) it comes with spaces, when I am trying to trim the spaces it is not allowing me to do that. Can you please help me out on that. As I have to reverse the output of the variable also. ---------- Post... (0 Replies)
Discussion started by: manish8484
0 Replies

3. Shell Programming and Scripting

The last argument contains spaces, how do I get it into a variable?

Gooday I have an argument string that contains 15 arguments. The first 14 arguments are easy to handle because they are separated by spaces ARG14=`echo ${ARGSTRING} | awk '{print $14}'` The last argument is a text that may be empty or contain spaces. So any ideas on how I get the last... (23 Replies)
Discussion started by: zagga
23 Replies

4. Shell Programming and Scripting

Variable with several values and spaces.

Hello all. I am a newb obviously and a bit stumped on this, so any help gratefully accepted. The script is extracting metadata from individual mp3 files, then (hopefully will be) sorting them into newly-created subdirectories. I have filtered out the relevant metadata and have the album names... (8 Replies)
Discussion started by: spoovy
8 Replies

5. Shell Programming and Scripting

Triming spaces for any variable

Hi, I want to trim the spaces on left side of the variable value. For eg: if it is 4 spaces followed by value, All the spaces on the left should be supressed. Easy but want it quickly. Regards, Shiv@jad (6 Replies)
Discussion started by: Shiv@jad
6 Replies

6. Shell Programming and Scripting

adding spaces for a variable value

Hi, i have to form the header and add fillers(spaces) to it. I have done something like this. i have added 10 spaces at the end HDR="AAAABBBBCCNN " echo $HDR >> file1.dat but the spaces are not being stored in the file. How to add the spaces. (2 Replies)
Discussion started by: dnat
2 Replies

7. Shell Programming and Scripting

appending spaces to a variable

Hi All, I have a requirement, in which i have to append some spaces to the variable, and then send it to another function. I am new to the UNIX shell programming. Ultimately the length of the string should be 40 characters. exp: Login = "rallapalli" (length = 10) i have to append 30 spaces to... (2 Replies)
Discussion started by: rallapalli
2 Replies

8. Solaris

spaces trimming while assigning to a variable

Hi my lovely friends, Im writing one pgm in which i trying to assign some values like $var='Jun 6' but if i do echo of this $var will trim the spaces expect one space. $echo $var $Jun 6 But if var='Jun 28', then this will works fine $echo $var $Jun 28 this is required to exctract... (2 Replies)
Discussion started by: Lokesha
2 Replies

9. Shell Programming and Scripting

Variable containing spaces

Hi, my var is: PATH_LOG=/opt/WebSphere/CR Comune Roma.log a filename which contains blank chars. How can I call it from prompt ? Ex: ls $PATH_LOG or cat $PATH_LOG tks, Carmen- (2 Replies)
Discussion started by: Carmen123
2 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies

Featured Tech Videos