LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script Help - If Statement (https://www.linuxquestions.org/questions/linux-newbie-8/script-help-if-statement-817206/)

genogp 06-30-2010 12:30 PM

Script Help - If Statement
 
Hi guys i need some help.
Im trying to run a command then grep a log file to see if the command was successful or not. In the log file there are specific words i can grep for to check for success or failure. Then weather it fails or succeeds i will send a mail to myself.


heres what i have;

Code:

#!/bin/bash

STOPSRV='stopServer.sh server1'
STARTSRV='startServer.sh server1'
CHECKA='grep' "Server server1 stopped" SystemOut.log
CHECKB='grep' "open for e-bus" SystemOut.log
MAILSUC='sendEmail -t test@mail.com -f 'host@mail.com' -u hello -s mail.smtp.com:25 -m bye'
MAILFAIL='sendEmail -t test@mail.com -f 'host@mail.com' -u fail -s mail.smtp.com:25 -m fail'

$STOPSRV
sleep 2m
if [[ "$CHECKA" == Server server1 stopped ]]
then $MAILSUC
else $MAILFAIL
fi

$STARTSRV
sleep 2m
if [[ "$CHECKB" == open for e-bus ]]
then $MAILSUC
else $MAILFAIL
fi

exit


Then if statement is were im falling short, and i think im putting the back ticks in the wrong place as well. Please help me

johnshen64 06-30-2010 01:09 PM

quote your multitoken strings and you should be fine.

e.g.

if [[ "$CHECKB" == "open for e-bus" ]]

instead of

if [[ "$CHECKB" == open for e-bus ]]

genogp 06-30-2010 01:29 PM

Thanks the if statement doesnt through an error anymore more but the statement is not working as expected.

The statement will only switch to the "else" therefore the variable "CHECKA" and "CHECKB" are failing.
Ive been starring at this for an hour and i know its something simple but cant put my finger on it :(

catkin 06-30-2010 09:40 PM

The line CHECKA='grep' "Server server1 stopped" SystemOut.log tells bash to set variable CHECKA to grep, to run the command "Server server1 stopped" SystemOut.log and then to unset CHECKA. It should have produced an error message as this experiment at the command prompt shows
Code:

c@CW8:~$ CHECKA='grep' "Server server1 stopped" SystemOut.log
bash: Server server1 stopped: command not found

Presumably your intention was to put the output of grep "Server server1 stopped" SystemOut.log into CHECKA. That could be done with this line
Code:

CHECKA=$( grep "Server server1 stopped" SystemOut.log 2>&1 )
You can figure a lot out for yourself by using echo statements to see what variables contain
Code:

echo "DEBUG: CHECKA: '$CHECKA'"

simon.sweetman 06-30-2010 11:32 PM

Most commands set the exit status, zero indicates success and non-zero indicates failure (different number for each possible type of error). In shell scripts $? is the exit status of the last command.

So if your scripts follow this convention (i.e. using exit 0 when everything is OK and exit 1 , exit 2, etc) you can test exit status after the command like:
Code:

$STOPSRV
ISOK=$?
sleep 2m
if [ $ISOK -eq 0 ]
then $MAILSUC
else $MAILFAIL
fi

Note that $? is stored in ISOK, if you wait till after the sleep statement $? will be the exit status of sleep no your script!

If your scripts don't set exit status correctly, grep does so you can do something like the following
Code:

$STOPSRV
grep "Server server1 stopped" SystemOut.log > /dev/null 2>&1
ISOK=$?
sleep 2m
if [ $ISOK -eq 0 ]
then $MAILSUC
else $MAILFAIL
fi


genogp 07-01-2010 01:49 AM

Many Thanks, guys you provided the solution

grail 07-01-2010 02:39 AM

Please mark as SOLVED once you have your solution.


All times are GMT -5. The time now is 06:50 PM.