[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: exit status in man page

From: Matthew Woehlke
Subject: Re: exit status in man page
Date: Tue, 03 Apr 2007 09:58:00 -0500
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv: Gecko/20070221 Thunderbird/ Mnenhy/

Brent Barker wrote:
my man page (1) for "grep" states:

Normally, exit status is 0 if selected lines are found and 1 otherwise. But the exit status is 2 if an error occurred, unless the -q or --quiet
      or --silent option is used and a selected line is found.

I'm using grep in a script, found here:

if echo "$1" | grep /
 echo "returned 1"
 echo "returned 0"

It seems that grep returns 1 if it finds the pattern, 0 if it does
not. This seems to be contrary to what is said in the man page. The
behavior is the same if I add the -q option.

Your assumptions are wrong. 0 is considered "success", anything else is considered failure. So your script above will echo "returned 1" when grep actually returns 0, and will echo "returned 0" when grep returns anything other than 0. Try this instead:

echo "$1" | grep / ; echo "returned $?"

This will display the actual return value, be it 0, 1, 2, 67*, or any other number. (*I don't think grep ever returns 67 :-). But some other program might.)

Obscurity: +5

reply via email to

[Prev in Thread] Current Thread [Next in Thread]