LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Check if actions in bash shell script are performed well (http://www.linuxquestions.org/questions/linux-newbie-8/check-if-actions-in-bash-shell-script-are-performed-well-703296/)

proNick 02-09-2009 03:40 AM

Check if actions in bash shell script are performed well
 
Hi all!

I have a shell script that creates MySQL dump, like:


#!/bin/bash

/usr/local/bin/mysqldump -hXXX -uYYY etc etc



I would like that if for some reasion dump is not properly created, script make attempts until it's done.

Can you help me how to do it?

Thank you in advance!

kauuttt 02-09-2009 03:50 AM

put the command in a loop...check for the dump file whether it is created or not..if it is created,break from the loop...

i92guboj 02-09-2009 04:05 AM

The right solution is most times to check the exit status of the given application. After all, that's what the exit status is for.

mysqldump will spit an integer when it's done running. Usually, 0 means success, and any other things means that something bad happened. The concrete value can be used for different errors. Usually, the man of info page should specify that.

In any case, this return code can be used in many ways to branch your script depending on the success of failure of a given instruction.

In addition, in bash, 0 is true, while anything else is false. So, you could perfectly do something like:

Code:

while ! <command>
do
  :
done

That will loop indefinitely until the exit status of <command> is zero (success).

You can even use the logical AND and OR operators to bifurcate :p

Code:

<command> && <to_do_if_success> || <to_do_if_not>

proNick 02-09-2009 06:55 AM

Thank you on your answers, but I need more informations. I will try to explain a bit more...

As we know, mysql dump is created with something like:

mysqldump -hmyhost -umyuname -ppasswd dbname > dbname.sql


But sometimes, this command fails to execute properly to the end.

So, how can I "catch" if error occured? It would be great if I can put error message in variable, and work with it's value.

Of course, I tryed to solve it by myself...

I can not do it with something like mysqldump ... 2> error, because I have to make output in file and in variable.

I also tryed to work with tee command, but no luck to solve this:

mysqldump -hXXX -uYYY -pZZZ 2>&1 1> mydump.sql | tee error.log


So, how can I work with last tee error.log as a variable (if possible without creating file error.log)?

Thank you in advance!

onebuck 02-09-2009 12:01 PM

Hi,

What part of 'i92guboj' post indicates exit code or status? Most properly written applications provide a 'exit code'. You will use that 'exit code' or status to decide within your script a specific operation to be performed relative to the indicated error.

i92guboj 02-09-2009 12:33 PM

proNick, the problem with saving the output to a variable, if you ask me, is that variables have a very finite space available. So, if anything nasty happens the output will probably not fit into your variable. If you really want to save the stuff for reference, redirect both stderr and stdout to a file, and for the rest, use the exit status to determine if the thing was successful. If it was, there's absolutely no point in parsing any output, is there? If the exit status is not zero, then you can go to the file and grep, sed, cut or awk it to your desire.

But if you are going to do intensive parsing in long logs, bash is probably not the best.

chrism01 02-09-2009 07:25 PM

Basic version
Code:

some_cmd >some_cmd.log 2>&1
if [[ $? -ne 0 ]]
then
    /bin/mail -s "$SUBJECT" "$EMAIL_ADDR" < some_cmd.log
fi

There's little point in making it keep trying if you don't know what's broken.


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