]> git.ipfire.org Git - thirdparty/man-pages.git/commitdiff
src/bin/grepc: Use the same error code as grep(1)
authorAlejandro Colomar <alx@kernel.org>
Wed, 29 Oct 2025 22:38:20 +0000 (23:38 +0100)
committerAlejandro Colomar <alx@kernel.org>
Sun, 2 Nov 2025 17:52:48 +0000 (18:52 +0100)
Exit with 2 on error.

If nothing matches, it's a success, and it exits with 0 normally.
This is different from grep(1), but I don't know how to write
the script to exit with 1 if nothing matches.

This implementation may also ignore some xargs(1) errors.  A future
commit --with code suggested by Paul Eggert-- will fix this.

Link: <https://software.codidact.com/posts/294883>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
src/bin/grepc

index ef1ea565e028d967ad3f9c08bab3e73318496b22..a1916dc49c4487814609105fa49ade81e82b4af8 100755 (executable)
@@ -3,7 +3,8 @@
 # Copyright, the authors of the Linux man-pages project
 # SPDX-License-Identifier: GPL-3.0-or-later
 
-set -uo pipefail;
+set -Eeuo pipefail;
+trap 'exit 2;' ERR;
 
 
 # Defaults:
@@ -47,7 +48,7 @@ x=''
 grepc_err()
 {
        >&2 printf '%s\n' "$(basename "$0"): error: $*";
-       exit 1;
+       exit 2;
 }
 
 
@@ -135,7 +136,7 @@ while getopts "A:B:C:chilm:nrt:x:" opt; do
                esac;
                ;;
        \? | *)
-               exit 1;
+               exit 2;
                ;;
        esac;
 done;
@@ -238,16 +239,18 @@ opts=($A $B $C $c $h $i $l -M $m $n);
 
 
 if test -z "$*"; then
-       pcre2grep "${opts[@]}" -f "$patterns";
+       pcre2grep "${opts[@]}" -f "$patterns" || true;
 else
        find "$@" -type f -print0 \
        | if test -z "$c"; then
                # shellcheck disable=SC2248  # $i may be -i or nothing.
-               xargs -0 grep -lZPI $i -- "$identifier";
+               xargs -0 grep -lZPI $i -- "$identifier" || true;
        else
                cat;
        fi \
-       | xargs -0 pcre2grep "${opts[@]}" -f "$patterns";
+       | {
+               xargs -0 pcre2grep "${opts[@]}" -f "$patterns" || true;
+       };
 fi \
 | if test "$r" = 'yes'; then
        perl -p -e 's/('"$identifier"')/\033[32m\1\033[0m/';