gnu/167009: GNU grep -q can exit !0 even if strings found

David Wolfskill david at catwhisker.org
Mon Apr 16 21:40:05 UTC 2012


>Number:         167009
>Category:       gnu
>Synopsis:       GNU grep -q can exit !0 even if strings found
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Apr 16 21:40:04 UTC 2012
>Closed-Date:
>Last-Modified:
>Originator:     David Wolfskill
>Release:        FreeBSD 8.3-PRERELEASE i386
>Organization:
Wolfskill & Dowling Residence
>Environment:
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


>Description:
	The -q flag for GNU grep is described as:

`-q'
`--quiet'
`--silent'
     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.

`-s'
`--no-messages'
     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'
     instead.

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

>How-To-Repeat:
	I encountered this in looking for certain pathnames in
	executables, thus:

albert(8.3-P)[1] which grep
/usr/bin/grep
albert(8.3-P)[2] 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)[3] grep -alw /usr/local /usr/local/bin/bison; echo $?
/usr/local/bin/bison
0
albert(8.3-P)[4] strings -a /usr/local/bin/bison | grep -w /usr/local; echo $?
/usr/local/lib
/usr/local/share/locale
/usr/local/share/bison
/usr/local/bin/gm4
/usr/local/lib
0
albert(8.3-P)[5] strings -a /usr/local/bin/bison | grep -qw /usr/local ; echo $? 
141
albert(8.3-P)[6] 

	Most of the folks to whom I've shown this have agreed that
	0 != 141.  :-}
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list