From ef01a3e3ef0f486c6872cd03fa92d00d8a931b7e Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Wed, 29 Oct 2025 23:38:20 +0100 Subject: [PATCH] src/bin/grepc: Use the same error code as grep(1) 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: Signed-off-by: Alejandro Colomar --- src/bin/grepc | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/bin/grepc b/src/bin/grepc index ef1ea565e..a1916dc49 100755 --- a/src/bin/grepc +++ b/src/bin/grepc @@ -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/'; -- 2.47.3