Reputation: 51
#!/bin/bash
BACKUP=backup_date
SOURCE=Arithmetic_Operators
echo "Taking backup from ${SOURCE} directory to backup directory ${BACKUP} .."
# Checking the source directory ${SOURCE} exists or not ! if not exists die
# Script is unsuccessful with exit status # 1
[ ! -d $SOURCE ] && echo "source directory $SOURCE not found"
exit 1
# Checking the backup directory ${BACKUP} exists or not ! if not exists die
# Script is unsuccessful with exit status # 2
[ ! -d $BACKUP ] && echo "backup directory $BACKUP not found"
exit 2
# Let Start the backing up
tar cvf $SOURCE $BACKUP 2> /wrong/logs.txt
if [ $? -ne 0 ]
then
# die with unsuccessful shell script termination exit status # 3
echo "An error occurred while making a source directory backup, see /wrong/logs.txt file".
exit 3
fi
This is my script to backing up the source directory(Arithmetic_Operators) to destination directory (backup_date), while running the script my script was ending with message
Taking backup from Arithmetic_Operators directory to backup directory backup_date ..
source directory Arithmetic_Operators not found
where i did mistake why this script is not running could you please help me on this ?
Upvotes: 0
Views: 134
Reputation: 753870
These lines mean the script exits unconditionally:
[ ! -d $SOURCE ] && echo "source directory $SOURCE not found"
exit 1
You probably meant:
if [ ! -d $SOURCE ]
then
echo "source directory $SOURCE not found" >&2
exit 1
fi
Or maybe:
[ ! -d $SOURCE ] && { echo "source directory $SOURCE not found" >&2; exit 1; }
Note that error messages should be sent to standard error, not standard output. It wouldn't be a bad idea to include the name of the script ($0
) in the messages; it helps identify which script generated/detected the problem.
You have a similar problem after checking the $BACKUP
directory.
Also, as a general rule, enclose variable references in double quotes:
[ ! -d "$SOURCE" ] && { echo "source directory $SOURCE not found" >&2; exit 1; }
(The second reference is already inside double quotes, of course.)
Upvotes: 1