LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 02-20-2012, 05:34 PM   #1
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Rep: Reputation: 16
shell script, rsync wrapper, space in path


I've been using this script in various forms for years now. It has a 'bad habit' as you'll see below. I think the time has come to make a significant change with the way I pass args to the main program but I'm unsure how to do that, and I'd like to understand technically why another method would work better.

Here's are just the critical snippets:

Code:
RSYNCARGS="-e ssh --delete -a -v --iconv=UTF-8,UTF8-MAC --exclude /var/lib/munin \
 --exclude /var/foo bar --rsync-path=/usr/local/bin/rsync"

for i in $FOO
do
 rsync $RSYNCARGS $i $TARGET_HOST:$TARGET_DIR1 >> $RSYNCLOG 2>&1
done
The critical part here is the 2nd 'exclude' that involves a space in the path. I've tried a variety of escapes, quotes, etc. (including \"/var/foo bar\") around the path but nothing works. I think these hacks didn't work partly because I'm passing all that stuff as a string. I'm thinking it would be best to just pass it all as an arg (eg, LSL='ls -l').

Any suggestions would be welcome. I'd like to better understand what would be best in terms of setting the value as a string, as a command, and whether using ${RSYNCARGS} would be helpful.

Cheers,
-zeek
 
Old 02-20-2012, 05:47 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,776

Rep: Reputation: 311Reputation: 311Reputation: 311Reputation: 311
The usual method to handle spaces in file names is to escape the space, like so:
--exclude /var/foo\ bar

Note the space between the \ and bar.
 
Old 02-20-2012, 05:57 PM   #3
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Original Poster
Rep: Reputation: 16
Thanks --I did try that and wound up with something like: 'rsync: link_stat "/path/to/bar" failed: No such file or directory (2)

I believe the problem is how the args are passed as a string.
 
Old 02-20-2012, 06:14 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,464

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
Quote:
Originally Posted by ezekieldas
I think these hacks didn't work partly because I'm passing all that stuff as a string. I'm thinking it would be best to just pass it all as an arg (eg, LSL='ls -l').
You could use a function:

Code:
RSYNC() {
 rsync -e ssh --delete -a -v --iconv=UTF-8,UTF8-MAC --exclude /var/lib/munin \
 --exclude '/var/foo bar' --rsync-path=/usr/local/bin/rsync "$@"
}

for i in $FOO
do
 RSYNC "$i" "$TARGET_HOST:$TARGET_DIR1" >> "$RSYNCLOG" 2>&1
done
or an array:
Code:
RSYNCARGS=(-e ssh --delete -a -v --iconv=UTF-8,UTF8-MAC --exclude /var/lib/munin \
 --exclude '/var/foo bar' --rsync-path=/usr/local/bin/rsync)

for i in $FOO # FOO might be better as an array as well: "${FOO[@]}"
do
 rsync "${RSYNCARGS[@]}" "$i" "$TARGET_HOST:$TARGET_DIR1" >> "$RSYNCLOG" 2>&1
done
and get into the habit of quoting your variables.
 
Old 02-20-2012, 07:33 PM   #5
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Original Poster
Rep: Reputation: 16
ntubski --thanks. Turning this into a function did the trick. Important thing to remember here is including "$@" when something follows :-)
 
Old 02-21-2012, 02:32 AM   #6
omgs
Member
 
Registered: Dec 2010
Posts: 64

Rep: Reputation: 6
I think the initial approach just needed to double quote the variable:
Code:
RSYNCARGS="-e ssh --delete -a -v --iconv=UTF-8,UTF8-MAC --exclude /var/lib/munin \
 --exclude /var/foo bar --rsync-path=/usr/local/bin/rsync"

for i in $FOO
do
 rsync "$RSYNCARGS" $i $TARGET_HOST:$TARGET_DIR1 >> $RSYNCLOG 2>&1
done
Double quoting the rest of variables, if they don't contain any spaces, looks unnecessary, but if you find problems, quote them, too.
 
Old 02-21-2012, 05:52 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,464

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
Quote:
Originally Posted by omgs View Post
I think the initial approach just needed to double quote the variable:
Nope, doesn't work:
Code:
~/tmp$ cat ~/bin/args.sh # a useful script to understand shell arg passing
#!/bin/sh
printf '{%s}\n' "$@"
~/tmp$ args.sh 1 2 3
{1}
{2}
{3}
~/tmp$ args.sh file1 "file with spaces" # suppose we want to pass 2 arguments, like this
{file1}
{file with spaces}
~/tmp$ ARGS="file1 file with spaces"
~/tmp$ args.sh $ARGS # unquoted variable doesn't work
{file1}
{file}
{with}
{spaces}
~/tmp$ args.sh "$ARGS" # quoted variable doesn't work
{file1 file with spaces}
~/tmp$ ARGS="file1 'file with spaces'"
~/tmp$ args.sh $ARGS # variable with quotes inside doesn't work
{file1}
{'file}
{with}
{spaces'}
~/tmp$ args.sh "$ARGS" # quoted variable with quotes inside doesn't work
{file1 'file with spaces'}
~/tmp$ passargs() { args.sh file 'file with spaces' ; }
~/tmp$ passargs # using a function works
{file}
{file with spaces}
~/tmp$ ARGS=(file1 'file with spaces')
~/tmp$ args.sh "${ARGS[@]}" # using a quoted array works
{file1}
{file with spaces}
Quote:
Double quoting the rest of variables, if they don't contain any spaces, looks unnecessary, but if you find problems, quote them, too.
I favour a policy of quoting unless you're certain you won't have problems, eg value can only be an integer anyway.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is my shell script that has rsync in it working? Cyberman Programming 4 08-31-2010 05:39 AM
[SOLVED] shell script wrapper for automating rsync someshpr Linux - Newbie 3 06-03-2010 01:31 AM
Shell script help: How to pass a directory path to the script? dv502 Programming 13 09-10-2008 03:07 AM
help on shell script as a wrapper to launch default programs according to input file daveiro Programming 2 11-24-2005 04:20 PM
local rsync shell script eradrix Programming 13 07-09-2005 12:57 AM


All times are GMT -5. The time now is 07:42 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration