From 1e94c940333b0d6794c904665c5d2f4911e778e5 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 12 Jun 2014 18:43:08 -0700 Subject: [PATCH] zgrep: exit with status 0 if a file matches and there's no trouble Reported by Pavel Raiskup in: http://bugs.gnu.org/17760 * zgrep.in (res): Treat exit status 0 to be greater than 1. Also, exit immediately on software configuration error. --- zgrep.in | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/zgrep.in b/zgrep.in index 857ea48..5c5102a 100644 --- a/zgrep.in +++ b/zgrep.in @@ -173,7 +173,7 @@ if test $# -eq 0; then fi exec 3>&1 -res=0 +res=1 for i do @@ -217,8 +217,23 @@ do fi >&3 5>&- ) r=$? - test 128 -lt $r && exit $r - test "$gzip_status" -eq 0 || test "$gzip_status" -eq 2 || r=2 - test $res -lt $r && res=$r + + # Ignore gzip status 2, as it is just a warning. + # gzip status 1 is an error, like grep status 2. + test $gzip_status -eq 2 && gzip_status=0 + test $gzip_status -eq 1 && gzip_status=2 + + # Use the more serious of the grep and gzip statuses. + test $r -lt $gzip_status && r=$gzip_status + + # Exit immediately on software configuration error. + test 126 -le $r && exit $r + + # Accumulate the greatest status, except consider 0 to be greater than 1. + if test $r -le 1 && test $res -le 1; then + test $r -lt $res + else + test $res -lt $r + fi && res=$r done exit $res -- 1.9.3