error handling in case sentences [Shell Script]

1

I have this setup and I need when an error happened in the main script, to take this error to a $INFO_PLUGIN temp file, the problem is the script continues working after case and send also an OK message to $INFO_PLUGIN temp file, overriding existing error message.

I would like to when a error comes, take this error to $INFO_PLUGIN temp file and prevent the execution of reportmail OK; in some way, but I don't know how and I need some help.

Thnaks in advance for your time and cooperation.

Main Script:

source ~/share/mailReport.fn

case $2 in
          firstcase)
               ssh -q -n loguser@server "mkdir -p /home/loguser/logstorage" 
               if [[ $? -ne 0 ]]; then
                    echo " ************ "
                    echo " *   Error  * "
                    echo " ************ "
                    reportmail 12;
                    exit 12;
               fi;
          ;;
          secondcase)
               startDate=`date +%s`
               if [ -z $3 ]; then
                    echo -e "\nInsert a parameter...: $0 $1 $2 [Here!]\n"
               exit;
               fi;
               echo -e "\nRunning test with error number...: $3\n"
               endDate=`date +%s`
               DIFF=$(( $endDate - $startDate ))
               echo -e "* Time in sec..: $DIFF\n"
               reportmail $3;
          ;;
esac
echo " ************ "
echo " * Finished * "
echo " ************ "

reportmail OK;

reportMail.sh Script:

case $1 in
         12)
            mailx -s "Error happened during mkdir" -r $SENDER_ADDRESS $RECIPIENT_ADDRESS
<<EOF
    Hello,
    Error happened in `uname -n` bla, bla, bla...
EOF
            echo -e "CRITICAL - Error creating destination dir" > $INFO_PLUGIN
         ;;
         OK)
            echo -e "OK - Time in sec..: $DIFF Total Saved..: `cat $temp` MB" > $INFO_PLUGIN
            rm $temp
         ;;
esac

Edit:

############################### Little example ###############################

test.sh

#!/bin/sh

source ~/test2.sh

case $1 in

        a) error 0; ;;
        b) error 1; ;;
        c) error 2; ;;

esac;

error 0; # This is what I need to jump in case of error

test2.sh:

#!/bin/sh

function error () {

case $1 in

        0) echo "Everything = OK" > ~/results.txt ;;
        1) echo "Sth Wrong = error 2" > ~/results.txt ;;
        2) echo "Sth Wrong = error 3" > ~/results.txt ;;

esac;
}

if I run:

[user@server dir]$ ./Test.sh 1

or:

[user@server dir]$ ./Test.sh 2

this is the result, included on file ~/results.txt

[user@server dir]$ cat results.txt
Everything = OK

The result is the same on all cases, because error 0; on first script, so my question is how can I do to determine if an error happened on some value on first case, for example: error happened running Test.sh b. How may I do to jump error 0; and prevent to execute it if there is an error before, else if there is no error on case. error 0; should be executed to say hey, everything is ok!

I hope you understand and thanks a lot for your time.

Adryoid

Posted 2014-02-03T10:51:33.283

Reputation: 38

Hi and welcome to SuperUser! You'll very likely find that it's easier to get answers to such questions if you can produce a minimal code sample exhibiting the unexpected behaviour. – l0b0 – 2014-02-03T13:30:51.897

In test.sh you state: error 0; # This is what I need to jump in case of error. So, is your question: How do I get a default branch in case...esac? If so, the answer is to use *) as the last case. – mpy – 2014-02-03T17:17:49.403

My question is:

if I run first script and no error happened, then error 0; will be executed at the end of script and send a OK Message to results.txt file. Otherwise if an error happened, ERROR message will be sent to results.txt by the seconf script but just for a second until first script run one more time error 0; and override ERROR message on results.txt. – Adryoid – 2014-02-04T09:09:35.467

Answers

0

Problem solved by adding this:

echo -e "CRITICAL - Error deleting files - Performance" > $INFO_PLUGIN && echo "1" > $err

this send a 1 to an error file and...

if [ `cat $err` -eq 1 ]; then
    reportmail 201$BYTES;
    rm $err
else
    reportmail 201$BYTES;
    reportmail OK;
    rm $err
fi;

this test if there was an error during execution of scripts, if yes, just send the report in ordr to see the log on a web server, otherwise, if everithing is OK send the log to a web server and send OK message to Nagios monitoring system.

Thanks! & until next time!

Adryoid

Posted 2014-02-03T10:51:33.283

Reputation: 38