LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-30-2010, 01:30 PM   #1
genogp
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Rep: Reputation: 0
Unhappy 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
 
Old 06-30-2010, 02:09 PM   #2
johnshen64
LQ Newbie
 
Registered: Jun 2010
Posts: 29

Rep: Reputation: 5
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 ]]

Last edited by johnshen64; 06-30-2010 at 02:10 PM. Reason: forgot to add example
 
Old 06-30-2010, 02:29 PM   #3
genogp
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Original Poster
Rep: Reputation: 0
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

Last edited by genogp; 06-30-2010 at 02:37 PM. Reason: didnt make sense
 
Old 06-30-2010, 10:40 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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'"
 
Old 07-01-2010, 12:32 AM   #5
simon.sweetman
Member
 
Registered: Mar 2009
Posts: 32

Rep: Reputation: 22
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
 
Old 07-01-2010, 02:49 AM   #6
genogp
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Many Thanks, guys you provided the solution
 
Old 07-01-2010, 03:39 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Please mark as SOLVED once you have your solution.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell script for adding a statement in a file after a particular statement TheIndependentAquarius Programming 4 06-28-2010 04:07 AM
[SOLVED] Or statement isn't working in script digity Linux - Newbie 8 02-09-2010 10:11 PM
If-statement in Shell Script SeraphimNL Linux - Newbie 4 01-11-2010 01:30 AM
Using apt-get in if statement in bash script jax8 Programming 4 03-29-2009 11:23 PM
Script and If Statement bspicer Linux - General 2 04-15-2002 07:02 AM


All times are GMT -5. The time now is 08:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration