zgrep: exit with status 0 if a file matches and there's no trouble
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 13 Jun 2014 01:43:08 +0000 (18:43 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Fri, 13 Jun 2014 01:43:30 +0000 (18:43 -0700)
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

index 857ea48c1d3ee74794edd75f62203cd8d45273ef..5c5102a328f26cd13b6de1f138e9bec767792ea5 100644 (file)
--- 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