shell script and variable problem
I have the following script I use for backup:
#######
#!/bin/sh
BACKUPLIST=/etc/backup.lst
HOSTNAME=`hostname`
YEAR=`date +%Y`
MONTH=`date +%B`
BACKUPDIR=/srv/misc/backup/${YEAR}/${MONTH}
DATE=`date +%Y%m%d`
mkdir -m 2775 -p $BACKUPDIR
for entry in `cat $BACKUPLIST`; do
name=`echo $entry | cut -d':' -f1`
startdir=`echo $entry | cut -d':' -f2`
bkdir=`echo $entry | cut -d':' -f3`
exlist=`echo $entry | cut -d':' -f4`
if [ -n $exlist -a -f $exlist ]; then
cd $startdir; tar -X $exlist -cf - $bkdir | bzip2 -c > ${BACKUPDIR}/${HOSTNAME}-${name}-${DATE}.tar.bz2
else
cd $startdir; tar cf - $bkdir | bzip2 -c > ${BACKUPDIR}/${HOSTNAME}-${name}-${DATE}.tar.bz2
fi
done
##########
Now, /etc/backup.lst has a list of directories I want to backup, and
looks like this
john:/home:john:/home/john/backupex.lst
vcs:/srv:/srv/vcs:
That is, some entries have 4th field, some has not.
The problem is that my test
if [ -n $exlist -a -f $exlist ]
does not work. For some reason, this always evaluate to true, and then
the tar command fails. Why is this?
My understanding is -n will check if $exlist is empty or not, and if it
is, it will fail. And even if it doesn't, -f $exlist should fail since
it wouldn't be able to find that file.
--
John L. Fjellstad
web: http://www.fjellstad.org/ Quis custodiet ipsos custodes
Reply to: