invoke one script based on previous script execution

I am database guy and not very good at shell scripts. I am seeking help to sharp my script coding. I have 5 scripts

  1. master script. I use this one to call other four scripts to do database work.
  2. db_backup_1 and log_backup_1
  3. 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

exit 0

thank you for your help in advance.

Please post the script with CODE tags an proper indentation to make it easier to read.

there is no reason to put the job in the background the way it is written.

don't convert $? to a string. it works but for the wrong reasons

this will do the same thing. you want to exit on error.

./db_backup_1.sh
rc=$?

echo "db_backup_1 returned ${rc}"
if [[ ${rc} -eq 0 ]]
then
   ./log_backup_1.sh
   # check $? here??
else
   mail -s "db_backup_1 Unsuccessful rc=${rc}" mymail@yahoo.com </dev/null >/dev/null 2>/dev/null
   exit 1
fi

that should give you a few things to think about.

frank:

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.

I hope this would help.

psshah:

Thanks to point out the syntax errors in my coding. It works. I need to spend more time on shell scripting.