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:

#!/bin/bash
if echo "$1" | grep /
then
 echo "returned 1"
else
 echo "returned 0"
fi

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.)

--
Matthew
Obscurity: +5



Reply via email to