Computing.Net > Forums > Unix > how to catch the exit status

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

how to catch the exit status

Reply to Message Icon

Name: Br1an
Date: October 25, 2002 at 21:00:37 Pacific
OS: Unix
CPU/Ram: 256
Comment:

Hi,

in a script, how to catch an exit status of a command, say egrep?

if i wanna know if this command
egrep "/foo/" foo.log
match any "foo" string in the file foo.log then what should i do to catch the exit status 0 of egrep ?



Sponsored Link
Ads by Google

Response Number 1
Name: Sean Miller
Date: October 25, 2002 at 22:56:36 Pacific
Reply:

Firstly, I assume you don't want to see the output from the grep? So you want to add the following to the end...

>/dev/null -- sends output to nowhere
2>&1 -- sends error messages to same as output.

Then you can use the shell variable $? to find the return code....

ie.

#!/bin/ksh

grep -i error errlog.txt >/dev/null 2>&1

if [ $? -eq 0 ]
then
echo "ALL QUIET ON THE WESTERN FRONT"
else
echo "SMOKE ON THE HORIZON!"
fi

Best Wishes,

Sean


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: how to catch the exit status

how to get the last field www.computing.net/answers/unix/how-to-get-the-last-field/6529.html

how to change the monitor resolution? www.computing.net/answers/unix/how-to-change-the-monitor-resolution/1256.html

how to map the j drive www.computing.net/answers/unix/how-to-map-the-j-drive/6227.html