LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash backup script - If multiple files starting with a exist problem (https://www.linuxquestions.org/questions/programming-9/bash-backup-script-if-multiple-files-starting-with-a-exist-problem-210943/)

demoncheese 07-29-2004 04:17 AM

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

demoncheese 07-29-2004 07:43 AM

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

osvaldomarques 07-29-2004 10:47 PM

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



All times are GMT -5. The time now is 12:05 PM.