PDA

View Full Version : Problem with use of variables in bash



qrwe
May 2nd, 2008, 12:12 PM
Hello,

I've created a simple bash script which makes gzipped tar dumps of specified directories. A snippet looks like this:

HOMEDIR="/mnt/backup"
BACKUPTYPE="daily"
SOURCE_LIST="/etc /var /usr"

tar cfz $HOMEDIR/$BACKUPTYPE_`date \+\%y\%m\%d`.tgz $SOURCE_LIST

The problem is that the dump has only been named with date afterwards, e.g. "080502.tgz". What am I doing wrong?
Thanks for any help!

tseliot
May 2nd, 2008, 12:56 PM
try this:

tar cfz $HOMEDIR/"$BACKUPTYPE"_$(date \+\%y\%m\%d).tgz $SOURCE_LIST

qrwe
May 5th, 2008, 08:38 AM
That made the trick, thank you! Will bash get the point if I nestle qoutes? E.g:

"tar cfz $HOMEDIR/"$BACKUPTYPE"_$(date \+\%y\%m\%d).tgz $SOURCE_LIST"

tseliot
May 5th, 2008, 09:48 AM
If you do this:

"tar cfz $HOMEDIR/"$BACKUPTYPE"_$(date \+\%y\%m\%d).tgz $SOURCE_LIST"

you're not telling bash to execute a command.

I added quotation marks so as to prevent bash from looking for $BACKUPTYPE_ instead of $BACKUPTYPE. Quotation marks helped for this reason.

grepster
May 5th, 2008, 10:17 AM
tar cfz $HOMEDIR/"$BACKUPTYPE"_$(date \+\%y\%m\%d).tgz $SOURCE_LIST

tar cfz "$HOMEDIR/${BACKUPTYPE}_$(date +%y%m%d).tgz"

grepster

omrsafetyo
May 5th, 2008, 06:18 PM
^ That was going to be my reccomendation - I think this is the best way to do it.


tar cfz "${HOMEDIR}/${BACKUPTYPE}_$(date +%y%m%d).tgz"

qrwe
May 8th, 2008, 10:21 AM
I never quit learning new things in Linux environments :-) Thanks all!