From david@catwhisker.org  Mon Apr 16 21:31:41 2012
Return-Path: <david@catwhisker.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id D474F106566B
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 16 Apr 2012 21:31:41 +0000 (UTC)
	(envelope-from david@catwhisker.org)
Received: from albert.catwhisker.org (m209-73.dsl.rawbw.com [198.144.209.73])
	by mx1.freebsd.org (Postfix) with ESMTP id 955148FC12
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 16 Apr 2012 21:31:41 +0000 (UTC)
Received: from albert.catwhisker.org (localhost [127.0.0.1])
	by albert.catwhisker.org (8.14.5/8.14.5) with ESMTP id q3GLVZTs012469
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 16 Apr 2012 14:31:35 -0700 (PDT)
	(envelope-from david@albert.catwhisker.org)
Received: (from david@localhost)
	by albert.catwhisker.org (8.14.5/8.14.5/Submit) id q3GLVZ8Z012468;
	Mon, 16 Apr 2012 14:31:35 -0700 (PDT)
	(envelope-from david)
Message-Id: <201204162131.q3GLVZ8Z012468@albert.catwhisker.org>
Date: Mon, 16 Apr 2012 14:31:35 -0700 (PDT)
From: David Wolfskill <david@catwhisker.org>
Reply-To: David Wolfskill <david@catwhisker.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: GNU grep -q can exit !0 even if strings found
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         167009
>Category:       gnu
>Synopsis:       grep(1): GNU grep -q can exit !0 even if strings found
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    jilles
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Apr 16 21:40:04 UTC 2012
>Closed-Date:    Tue Apr 17 22:06:44 UTC 2012
>Last-Modified:  Tue Apr 17 22:06:44 UTC 2012
>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@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:

From: Jilles Tjoelker <jilles@stack.nl>
To: bug-followup@FreeBSD.org, david@catwhisker.org
Cc:  
Subject: Re: gnu/167009: grep(1): GNU grep -q can exit !0 even if strings
 found
Date: Tue, 17 Apr 2012 21:08:12 +0200

 FreeBSD PR gnu/167009:
 > [strings -a /usr/local/bin/bison | grep -qw /usr/local fails]
 
 The peculiar thing is the shell, tcsh.
 
 What happens is that grep -q terminates early, so that strings receives
 SIGPIPE when it tries to write further data. Whereas the exit status
 of a pipeline in sh is always the exit status of the last element so
 that your command has the expected behaviour, this is different in tcsh.
 Tcsh looks backwards for a failing command if the last element has exit
 status 0, and returns the 141 corresponding to SIGPIPE.
 
 If you do not use -q, grep reads all of its input and strings will not
 be hit by SIGPIPE, so the exit status of the pipeline is the exit status
 of grep.
 
 Something similar happens if 'set -o pipefail' is in effect in bash or
 ksh93.
 
 I can't help but be happy that I'm not using tcsh, because
   tcsh -c 'yes | :'
 does return 0 (apparently it doesn't look backwards if the last element
 is a builtin) and
   tcsh -c 'yes | if (1) true'
 hangs. Also, the double prompt I see after typing
   yes | :
 in an interactive tcsh doesn't really inspire confidence.
 
 -- 
 Jilles Tjoelker

From: David Wolfskill <david@catwhisker.org>
To: Jilles Tjoelker <jilles@stack.nl>
Cc: bug-followup@FreeBSD.org
Subject: Re: gnu/167009: grep(1): GNU grep -q can exit !0 even if strings found
Date: Tue, 17 Apr 2012 12:12:15 -0700

 --5V5c01chtBAiSHoy
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 Content-Transfer-Encoding: quoted-printable
 
 Huh.  Thanks for the explanation.  :-}
 
 Peace,
 david
 --=20
 David H. Wolfskill				david@catwhisker.org
 Depriving a girl or boy of an opportunity for education is evil.
 
 See http://www.catwhisker.org/~david/publickey.gpg for my public key.
 
 --5V5c01chtBAiSHoy
 Content-Type: application/pgp-signature
 Content-Disposition: inline
 
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v2.0.19 (FreeBSD)
 
 iEYEARECAAYFAk+NwI4ACgkQmprOCmdXAD1D9wCeJwFWkjE9cHPAeFSk3x0wTSgf
 8wMAnA1SYowMAAcyvF3He4pz9NhkjrcS
 =UOp+
 -----END PGP SIGNATURE-----
 
 --5V5c01chtBAiSHoy--
State-Changed-From-To: open->closed 
State-Changed-By: jilles 
State-Changed-When: Tue Apr 17 22:05:26 UTC 2012 
State-Changed-Why:  
This is not a bug but a result of exit status of tcsh pipelines. 


Responsible-Changed-From-To: freebsd-bugs->jilles 
Responsible-Changed-By: jilles 
Responsible-Changed-When: Tue Apr 17 22:05:26 UTC 2012 
Responsible-Changed-Why:  
Take. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=167009 
>Unformatted:
