LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 05: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, 06:43 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
1. you're missing an ending double-quote ' " '
2. what do you mean by 'short way' ?
Need more info/examples
 
Old 08-10-2012, 07: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, 08:30 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 09:20 AM   #5
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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, 11:10 AM   #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, 12:15 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 12:51 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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, 02: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, 03:07 PM   #10
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

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, 03: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, 08:44 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 01: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, 03:12 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 04: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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:11 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
Open Source Consulting | Domain Registration