LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   capturing error codes in bash script (https://www.linuxquestions.org/questions/linux-general-1/capturing-error-codes-in-bash-script-634158/)

nikaudio 04-09-2008 07:10 AM

capturing error codes in bash script
 
Hello,
I have wrote script to replicate mysql databases from one server to another:

#!/bin/sh

mysql_path="/root/scripts/cron"
#log_file contain mysql databases which should be replicated
log_file="/root/scripts/logs/replication.log"

DATE=`date`
cd $mysql_path
databases_file=$mysql_path/databases_to_replicate

cat $databases_file | while read line; do mysqldump --lock-tables -u root -ppassword --opt "${line}" | mysql -u root -ppassword -h server2_name "${line}" ; done
RETVAL=$?
if [ $RETVAL = 0 ]
then echo "$DATE - replication OK" >> $log_file
else echo "$DATE - replication error" >> $log_file
cat $databases_file | mail -s "server2_name: mysql replication error" admin@domain.com
fi

I have problem capturing errors from that script.
When mysql database is not working on second server2_name script is working (return error) but when mysql database is not working o a server where mysqldump is done it returns OK.
How do I capture error from mysqldump ?
Regards
Slawomir

bigrigdriver 04-09-2008 08:33 AM

Code:

if [ $RETVAL = 0 ]
The single = is used for value assignment to a variable. It doesn't work in a decision loop to test for equality. Equality is expressed as == or -eq. == is used for string comparison; -eq is used for interer comparison.

Try this instead:
Code:

if [ $RETVAL -eq 0 ]

nikaudio 04-09-2008 08:56 AM

Problem lies in that I cannot capture error code return by mysqldump.
It is capturing error code only for mysql command.
Comparing it to 0 works either by [ $RETVAL = 0 ] or by [ $RETVAL -eq 0 ]

cat $databases_file | while read line; do (mysqldump --lock-tables -u slawek -p6
sn7vt231 --opt "${line}"; RETVAL1=$?) | mysql -u slawek -p6sn7vt231 -h 192.168.1
.126 "${line}" ; done
RETVAL2=$?
echo $RETVAL1
echo $RETVAL2

RETVAL1 would return nothing not initiated
RETVAL2 would return value


All times are GMT -5. The time now is 01:28 AM.