Operating System - HP-UX
1752590 Members
3573 Online
108788 Solutions
New Discussion юеВ

Why return code is not 0 on this script ?

 
SOLVED
Go to solution
Sammy_2
Super Advisor

Why return code is not 0 on this script ?

Why do I get return code of 1 when I grep -v (all lines) in a file ? If there is something left in the file , then I get 0 return code.

# cat test
echo hi
echo I am here

# grep -v echo test
# echo $?
1

Also
good judgement comes from experience and experience comes from bad judgement.
3 REPLIES 3
Matti_Kurkela
Honored Contributor
Solution

Re: Why return code is not 0 on this script ?

The "-v" option inverts the sense of the search, i.e. searching for lines that _don't_ match the given pattern.

Your file "test" does not contain any lines that _don't_ have the word "echo" in them, so the search result is "nothing acceptable was found". The return code is set to 1 to indicate that.

MK
MK
Sammy_2
Super Advisor

Re: Why return code is not 0 on this script ?

Thanks Matti. Makes sense.
good judgement comes from experience and experience comes from bad judgement.
James R. Ferguson
Acclaimed Contributor

Re: Why return code is not 0 on this script ?

Hi:

The return code from 'grep' will be:

0 = One or more matches found.
1 = No match found.
2 = Syntax error or inaccessible file (even if matches were found)

In your case, there are no lines that *don't* have the string "echo" so there are no matches and thus the return code is one (1).

Regards!

...JRF...