I am database guy and not very good at shell scripts. I am seeking help to sharp my script coding. I have 5 scripts
master script. I use this one to call other four scripts to do database work.
db_backup_1 and log_backup_1
db_backup_2 and log_backup_2
in master script, I want to call db_backup_1 first. If this one run successfully, then server return 0, then invoke log_backup_1. If it failed, return 1, a email will be sent to me.
Then sleep 30 seconds
invoke db_backup_2, if it is succesul, return 0, invoke log_backup_2. If failed, return 1, another email will be sent to me.
My initial code like this. I know it must contains problems. I seek guru to help me to sharp this piece of code and make it work. Then I will do my test.
#!/bin/ksh
./db_backup_1.sh &
pid_db_backup_1=$!
wait $pid_db_backup_1
echo "db_backup_1 returned $?"
If ["$?" -eq "0" ]; then
./log_backup_1.sh &
else
mail -s 'db_backup_1 Unsuccessful' mymail@yahoo.com
fi
sleep 30
./db_backup_2.sh &
pid_db_backup_2=$!
wait $pid_db_backup_2
echo "db_backup_2 returned $?"
If ["$?" -eq "0" ]; then
./log_backup_2.sh &
else
mail -s 'db_backup_2 Unsuccessful' mymail@yahoo.com
fi
Thanks for your advice. I think your code really works. I implemted code in master_script like this :
#!/bin/sh
echo "start to backup database"
./db_backup.sh
rc=$?
echo "db_backup.sh returned ${rc}"
if [[${rc} -eq 0 ]]
then
echo "database backup successful"
./log_backup.sh
else
mail -s " database backup Unsuccessful rc=${rc}" myemail@yahoo.com </dev/null
exit 1
fi
after db_backup-1 completed. testing exit failed. here is the error message. Please advice.
db_backup.sh returned 0
./master_script.sh[14]: [[0: not found.
database backup failed
mail: illegal option -- s
usage: mail [+] [-epqr] [-f file]
mail [-dt] persons
Issue 1:
Put space between "[" and "$"; something like below:
if [[ ${rc} -eq 0 ]]
Issue 2: Syntax for mail command is not correct.
This may vary depending upon type of OS. First try sending mail from command prompt, after that update proper syntax in script.