LinuxQuestions.org
Help answer threads with 0 replies.
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 08-09-2012, 06:48 PM   #1
samiel
LQ Newbie
 
Registered: Aug 2012
Posts: 12

Rep: Reputation: Disabled
Select multiple directory in bash script


I have to set a variable $SOURCE for a bash script
How can I write in a short way
Code:
$SOURCE="{~/aaa,~/bbb,~/ccc,...,~/zzz}
where aaa, bbb etc are directories?
Thanx
samiel
 
Old 08-09-2012, 07:43 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
1. you're missing an ending double-quote ' " '
2. what do you mean by 'short way' ?
Need more info/examples
 
Old 08-10-2012, 08:30 AM   #3
samiel
LQ Newbie
 
Registered: Aug 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
I've a script to backup some directories in my home.
This is the script (abridged):
Code:
#!/bin/bash

SOURCE="{argomentare/*,articoli/*}"
TARGET="/media/CORSAIR"
date=`date "+%y-%M-%D"`
TAG="backup"

if mount | grep -q $TARGET
then
	echo "Il disco  montato: eseguo il backup"
	logger Inizio backup di $SOURCE per `hostname` -t $TAG
	rsync -rvu --progress $SOURCE $TARGET/
	RSYNCERR=$?
	logger Fine backup per `hostname`: rsync ha restituito $RSYNCERR -t $TAG
	exit 0
else
	echo "Il disco non  montato"
	logger Impossibile eseguire il backup di `hostname`: disco non montato -t $TAG
	exit 1
fi
but it gives me this error:
Code:
samiel@debian:~$ bash -x backup.sh
+ SOURCE='{argomentare/*,articoli/*}'
+ TARGET=/media/CORSAIR
++ date +%y-%M-%D
+ date=12-29-08/10/12
+ TAG=backup
+ mount
+ grep -q /media/CORSAIR
+ echo 'Il disco  montato: eseguo il backup'
Il disco  montato: eseguo il backup
++ hostname
+ logger Inizio backup di '{argomentare/*,articoli/*}' per debian -t backup
+ rsync -rvu --progress '{argomentare/*,articoli/*}' /media/CORSAIR/
sending incremental file list
rsync: change_dir "/home/samiel//{argomentare/*,articoli" failed: No such file or directory (2)

sent 12 bytes  received 12 bytes  48.00 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1070) [sender=3.0.9]
+ RSYNCERR=23
++ hostname
+ logger Fine backup per debian: rsync ha restituito 23 -t backup
+ exit 0
SO I thing there is something wring in $SOURCE

Thanx
 
Old 08-10-2012, 09:30 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
I guess the question you need to ask yourself is, would the following work if you ran it as is on the command line:
Code:
rsync -rvu --progress '{argomentare/*,articoli/*}' /media/CORSAIR/
Assuming not, you then need to ask yourself what would you type on the command line? This will then help with what you need to store in the variable.
Initially I would probably suggest using an array.
 
Old 08-10-2012, 10:20 AM   #5
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
you might want to look into creating an includes option in your excludes.txt file:

so create a file called excludes.txt and it should look something like this:

Code:
+ foo/
+ foo2/
- foo3/
the + tells rsync to add the file or directory while the - tells it to ignore.

rysnc -rvu -X /path/to/excludes.txt source destination

should do it for you.
 
Old 08-10-2012, 12:10 PM   #6
samiel
LQ Newbie
 
Registered: Aug 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
I would probably suggest using an array
Can you show me an example?

Thanx
 
Old 08-10-2012, 01:15 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Quote:
Can you show me an example?
First you need to show what you would enter on the command line and confirm it works?

Also, lleb's suggestion was worth investigating too.
 
Old 08-10-2012, 01:51 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
If the arguments are to be evaluated locally, by bash, then the wild cards and brace expansion shouldn't be in quotes.
If you precede a connand with set, you can examine the arguments $1, $2, etc. to verify what arguments the command sees after bash performs the various expansions.
 
Old 08-10-2012, 03:23 PM   #9
samiel
LQ Newbie
 
Registered: Aug 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Il I had all this theorical knowledge,
I would be able to write that array.
If I ask for an example, that is because
I'm not able to write this script by myself...

s
 
Old 08-10-2012, 04:07 PM   #10
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
Why cant you just put a bunch a directory locations in a variable and go through it with a for loop?
 
Old 08-10-2012, 04:30 PM   #11
samiel
LQ Newbie
 
Registered: Aug 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
I don't need to backup all my home,
but only some selected directories.
Is there no way to tell bash to select them
in a short way? You suggested me some solutions,
but I dont understand what is the more convenient,
and in any case I've no experience
in bash programming... Any concrete example
will be welcome :-)

S
 
Old 08-11-2012, 09:44 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
You are missing my point. I am not asking you to write arrays or any more code. What I am asking is for you to write the command line entry needed to perform the task you are attempting.
For example:
Code:
rsync -rvu --progress old_dir/* new_dir
I am not confirming the above works, but my main point is, what does work and do you really require the wildcards?
 
Old 08-11-2012, 02:59 PM   #13
samiel
LQ Newbie
 
Registered: Aug 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Code:
rsync -rvu --progress old_dir/* new_dir
works. My problem is to choose more directories
in a economical way. I'd like to backup some directories
old_dir1,old_dir2,...,old_dir15

S
 
Old 08-12-2012, 04:12 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
So again, what would it look like on the command line for more than one directory? Also, do you require the asterisk (wildcard)?
 
Old 08-12-2012, 05:37 AM   #15
samiel
LQ Newbie
 
Registered: Aug 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
From console in the following form the command works correcly (yes, without wildcards...):
Code:
samiel@debian:~$ rsync -rvu --progress {bibliografie,documenti} prova
It doesn't work fron bash script:
Code:
#!/bin/bash
SOURCE="{argomentare,bibliografie,documenti}"
TARGET="/media/CORSAIR"		# Directory di destinazione dove cui fare il backup
date=`date "+%y-%M-%D"` # Data del backup; %H_%M per avere anche l'orario
TAG="backup" # tag dei log scritti in /var/log/messages
if mount | grep -q $TARGET
then
	echo "Il disco  montato: eseguo il backup"
	logger Inizio backup di $SOURCE per `hostname` -t $TAG
	rsync -rvu --progress $SOURCE $TARGET/
	RSYNCERR=$?
	logger Fine backup per `hostname`: rsync ha restituito $RSYNCERR -t $TAG
	exit 0
else
	echo "Il disco non  montato"
	logger Impossibile eseguire il backup di `hostname`: disco non montato -t $TAG
	exit 1
fi
bacause it tells:
Code:
samiel@debian:~$ bash -x backup.sh
+ SOURCE='{argomentare,bibliografie,documenti}'
+ TARGET=/media/CORSAIR
++ date +%y-%M-%D
+ date=12-34-08/12/12
+ TAG=backup
+ mount
+ grep -q /media/CORSAIR
+ echo 'Il disco  montato: eseguo il backup'
Il disco  montato: eseguo il backup
++ hostname
+ logger Inizio backup di '{argomentare,bibliografie,documenti}' per debian -t backup
+ rsync -rvu --progress '{argomentare,bibliografie,documenti}' /media/CORSAIR/
sending incremental file list
rsync: link_stat "/home/samiel/{argomentare,bibliografie,documenti}" failed: No such file or directory (2)
sent 12 bytes  received 12 bytes  48.00 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1070) [sender=3.0.9]
+ RSYNCERR=23
++ hostname
+ logger Fine backup per debian: rsync ha restituito 23 -t backup
+ exit 0
whre you can see in particular:
Code:
rsync: link_stat "/home/samiel/{argomentare,bibliografie,documenti}" failed: No such file or directory (2)
S
 
  


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
Script to Hit 'Enter' key multiple times in bash script. nithinmp Ubuntu 7 07-19-2012 08:01 AM
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 03:40 AM
Bash script: how do I select second-to-last argument in a list Robert S Linux - Software 2 11-23-2007 04:06 PM
Bash script to strip a certain directory out of directories in a directory? rylan76 Linux - General 3 08-29-2006 12:35 PM
making select show its menu in a bash script? zidane_tribal Programming 6 05-02-2005 06:52 AM


All times are GMT -5. The time now is 02:53 AM.

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