I am running this while
loop in a script to take mysqldump
and compress it, but I want to exit
the script if the table
doesn't exist. Below is what I have tried.
while read TABLES; do
sudo mysqldump $DB $TABLES | gzip -f > $DB.$TABLES.sql.gz
if [ $? != 0 ]; then
echo "mysqldump Query executed with error !!"
exit 1
fi
done < file
But this will give the exit status of gzip -f
, but not of mysqldump
. I know I can get the exit status of mysqldump
if I am not using gzip
there, but is there any way in this method to get the exit ststus for mysqldump
?
. I used like
while read TABLES; dosudo mysqldump $DB $TABLES | gzip -f > $DB.$TABLES.sql.gz
; if [ ${PIPESTATUS[0]} -ne 0 ];then echo "mysqldump Query executed with error !!";exit 1;fi;done < file`. Am I doing something wrong?backticks
in the script which was causing the issue. Removing it will work. Thanks @rakesh – prado Feb 13 '17 at 06:46gzip
will give an error so you also need to check for${PIPESTATUS[1]} -ne 0
. The result is an array${PIPESTATUS[@]}
=0 0
. – hschou Feb 13 '17 at 16:11