LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-15-2015, 09:08 AM   #1
vrltwe
LQ Newbie
 
Registered: Jul 2013
Posts: 11

Rep: Reputation: Disabled
Bash string substitution (rsync exclude)


I'm preparing a rsync script to backup from one machine to another through ssh. For this I'm successful with the following:

IP=150.163.49.15
ORIGIN=/home
DESTINATION=samba

rsync -avzP --exclude={cristiano,marcelo} root@$IP:$ORIGIN $DESTINATION/

I would like to improve it, moving the exclude part to a variable also. I've unsuccessfully tried this:

IP=150.163.49.15
ORIGIN=/home
DESTINATION=samba
EXCLUDE={cristiano,marcelo}

rsync -avzP --exclude=$EXCLUDE root@$IP:$ORIGIN $DESTINATION/

What is wrong with the EXCLUDE syntax?
 
Old 05-15-2015, 01:38 PM   #2
warshall
LQ Newbie
 
Registered: Apr 2015
Posts: 3

Rep: Reputation: Disabled
Hi-

Your problem is that bash expands braces before variables. So your effective command line (the one rsync is actually getting) includes the literal string --exclude={cristiano,marcelo}, as opposed to the brace-expanded version of that, namely --exclude=cristiano --exclude=marcelo, which would be what you want.

You might want to use an array. I use zsh, not bash, but on my system, after
Code:
EXCLUDE=(cris marc)
,
Code:
${EXCLUDE/#/--exclude=}
expands to --exclude=cris --exclude=marc.

Hope this helps,

Andrew Warshall
 
1 members found this post helpful.
Old 05-15-2015, 01:51 PM   #3
jhekkanen
LQ Newbie
 
Registered: Dec 2014
Location: Finland
Posts: 3

Rep: Reputation: Disabled
I'm assuming the Bash shell here. I'm not sure about all the details of
this but when you run the first command you are using brace expansion:

Code:
bash-4.2$ echo cmd --exclude={foo,bar}
cmd --exclude=foo --exclude=bar
Now when we introduce the variable, we can see where things go wrong:

Code:
bash-4.2$ EXCLUDE={foo,bar}
bash-4.2$ echo cmd --exclude=$EXCLUDE
cmd --exclude={foo,bar}
One option would be to use an array instead:

Code:
bash-4.2$ EXCLUDE=(foo bar)
bash-4.2$ echo cmd ${EXCLUDE[@]/#/--exclude=}
cmd --exclude=foo --exclude=bar

Last edited by jhekkanen; 05-15-2015 at 01:53 PM.
 
1 members found this post helpful.
Old 05-15-2015, 07:15 PM   #4
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,314

Rep: Reputation: 568Reputation: 568Reputation: 568Reputation: 568Reputation: 568Reputation: 568
How about
Code:
EXCLUDE=$(echo --exclude={cristiano,marcelo})
echo rsync -avzP $EXCLUDE root@$IP:$ORIGIN $DESTINATION
gives
Code:
rsync -avzP --exclude=cristiano --exclude=marcelo root@150.163.49.15:/home samba
 
1 members found this post helpful.
  


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
complex bash string substitution of csv file with multiline data Ryan_ Programming 16 03-31-2012 10:48 AM
[SOLVED] Bash string substitution porphyry5 Programming 1 05-14-2011 01:08 AM
string substitution lipun4u Programming 11 01-13-2011 01:47 AM
[SOLVED] Bash: functional difference between process substitution and here string with $( ) ? catkin Programming 2 03-15-2010 02:08 AM
String Substitution Swapna173 Linux - Newbie 1 08-04-2008 06:22 AM


All times are GMT -5. The time now is 01:46 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration