gnu/167009: GNU grep -q can exit !0 even if strings found
david at catwhisker.org
Mon Apr 16 21:40:05 UTC 2012
>Synopsis: GNU grep -q can exit !0 even if strings found
>Arrival-Date: Mon Apr 16 21:40:04 UTC 2012
>Originator: David Wolfskill
>Release: FreeBSD 8.3-PRERELEASE i386
Wolfskill & Dowling Residence
System: FreeBSD albert.catwhisker.org 8.3-PRERELEASE FreeBSD 8.3-PRERELEASE #486 234278M: Sat Apr 14 04:01:03 PDT 2012 root at freebeast.catwhisker.org:/common/S1/obj/usr/src/sys/ALBERT i386
The -q flag for GNU grep is described as:
Quiet; do not write anything to standard output. Exit immediately
with zero status if any match is found, even if an error was
detected. Also see the `-s' or `--no-messages' option.
Suppress error messages about nonexistent or unreadable files.
Portability note: unlike GNU `grep', traditional `grep' did not
conform to POSIX.2, because traditional `grep' lacked a `-q'
option and its `-s' option behaved like GNU `grep''s `-q' option.
Shell scripts intended to be portable to traditional `grep' should
avoid both `-q' and `-s' and should redirect output to `/dev/null'
Despite this, there are cases where the use of "grep" to find a
string will succeed (with an exit status of 0), while an
otherwise-identical search using "grep -q" will merely exit with
a non-zero status code.
While it's possible that I'm confused, that seems to contradict
the above info excerpt. It's also possible that there's
something peculiar about the FreeBSD environment (from the
perspective of the GNU folks).
I encountered this in looking for certain pathnames in
albert(8.3-P) which grep
albert(8.3-P) grep --version
grep (GNU grep) 2.5.1-FreeBSD
Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
albert(8.3-P) grep -alw /usr/local /usr/local/bin/bison; echo $?
albert(8.3-P) strings -a /usr/local/bin/bison | grep -w /usr/local; echo $?
albert(8.3-P) strings -a /usr/local/bin/bison | grep -qw /usr/local ; echo $?
Most of the folks to whom I've shown this have agreed that
0 != 141. :-}
More information about the freebsd-bugs