LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-29-2004, 04:17 AM   #1
demoncheese
LQ Newbie
 
Registered: Aug 2003
Location: South East England
Distribution: Suse 9.1, Mandrake 9.2 & 10, Fedora Core 2 & IPCop 1.3
Posts: 11

Rep: Reputation: 0
Bash backup script - If multiple files starting with a exist problem


Hi,

I am trying to get a backup script working to tar and zip people's home directories based on each letter of the alphabet.

The code I have is:

letters="a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4
5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"

for i in `echo $letters`
do
if [ -d /home/"$i"* ]
then
tar -czf "$BUP_DIR"home_"$i".tgz /home/"$i"* >> $BUP_LOG 2> /dev/null
echo "/home/$i* tarred and zipped" >> $BUP_LOG
else
echo "No Users starting with $i exist" >> $BUP_LOG
fi
done

The problem is that the if statement falls over if there is more than one user starting with that letter. The rest of the statment works fine if I remove the if but I get a load of empty .tgz files where there are no users.

Can anyone let me know how I can say 'if there are any directories starting with a then archive them in a.tgz'?

Thanks
 
Old 07-29-2004, 07:43 AM   #2
demoncheese
LQ Newbie
 
Registered: Aug 2003
Location: South East England
Distribution: Suse 9.1, Mandrake 9.2 & 10, Fedora Core 2 & IPCop 1.3
Posts: 11

Original Poster
Rep: Reputation: 0
Problem sorted.

Sorted the problem now.

For anyone interested or having the same problem this is the modified code that works:

letters="a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4
5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"

for i in `echo $letters`
do
ls -al /home/$i* &> /dev/null
if [ $? == 0 ]
then
tar -czf "$BUP_DIR"home_"$i".tgz /home/"$i"* >> $BUP_LOG 2> /dev/null
echo "/home/$i* tarred and zipped" >> $BUP_LOG
else
echo "No Users starting with $i exist" >> $BUP_LOG
fi
done
 
Old 07-29-2004, 10:47 PM   #3
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi demoncheese,
I would use another approach to do the same task
Code:
for i in `ls -1 /home | cut -c1 | sort -u`
do
...
done
 
  


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
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
Backup script in bash gauge73 Programming 13 10-17-2005 06:25 AM
Backup Script from Multiple Drives to one Big Drive robinhood1995 LinuxQuestions.org Member Success Stories 5 03-26-2005 06:17 PM
problem creating BASH script with files that have whitespace scattaneo Linux - General 2 11-06-2004 05:40 PM
bash shell backup script turnip Programming 0 04-03-2003 06:00 PM


All times are GMT -5. The time now is 11:38 AM.

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