grep's exit codes on valid match >0

For example:
Code:
# cat /sys/conf/NOTES | grep -Em1 '^[a-z]+[[:blank:]]+[a-z]+$'
Exit code: [B]141[/B]
But there are also other examples, of various non zero exit codes, on success.

This caused me problems
 
Not according to grep(1):
DIAGNOSTICS
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.
 
Your example returns zero on exit on my machine, RELEASE-8.2 i386. I'm using shells/bash as shell.

Code:
[kimmo@firewall ~]$ cat /sys/conf/NOTES | grep -Em1 '^[a-z]+[[:blank:]]+[a-z]+$'
device          loop
[kimmo@firewall ~]$ echo $?
0
[kimmo@firewall ~]$
 
SirDice said:
Not according to grep(1):
I've read that.
Have you copied and executed my code, on command line? (Setup your shell, to show you exit codes of last commands)

I've received exit status of 2, when I've also received valid match, but that was because in dir there was an symlink that pointed to deleted file.

Here is another example (tcsh)
Code:
# cat /sys/conf/NOTES | grep -Em1 '^device'                  0 /root/sh_testground
device          hwpmc                   # Driver (also a loadable module)
blackhole#                                                         141 /root/sh_testground
When I remove m1, I get exit code 0
Code:
# cat /sys/conf/NOTES | grep -E '^device'
... lots of output ...
0 /root/sh_testground

All this on 8.1-RELEASE-p2
 
Yep, on tcsh I get 141 too.

Looking at the code of grep:
Code:
/* We register via atexit() to test stdout.  */
  exit (errseen ? 2 : status);

Status is actually the return from the grepfile function, which seems to return a count in some cases.
 
It can be some bits in $? pointing if program had coredump or not etc..
This regexp works good for me in csh (0,1,2 retcodes)
 
Back
Top