From unknown Sun Aug 17 10:18:09 2025 X-Loop: help-debbugs@gnu.org Subject: bug#54035: Patch for easier use in scripting pipelines Resent-From: Ulrich Eckhardt Original-Sender: "Debbugs-submit" Resent-CC: bug-grep@gnu.org Resent-Date: Thu, 17 Feb 2022 07:58:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 54035 X-GNU-PR-Package: grep X-GNU-PR-Keywords: To: 54035@debbugs.gnu.org X-Debbugs-Original-To: bug-grep@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.164508465129740 (code B ref -1); Thu, 17 Feb 2022 07:58:01 +0000 Received: (at submit) by debbugs.gnu.org; 17 Feb 2022 07:57:31 +0000 Received: from localhost ([127.0.0.1]:50016 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nKbfO-0007jc-Ho for submit@debbugs.gnu.org; Thu, 17 Feb 2022 02:57:31 -0500 Received: from lists.gnu.org ([209.51.188.17]:47822) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nKbfM-0007jU-F9 for submit@debbugs.gnu.org; Thu, 17 Feb 2022 02:57:29 -0500 Received: from eggs.gnu.org ([209.51.188.92]:44252) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nKbfM-0004wq-1o for bug-grep@gnu.org; Thu, 17 Feb 2022 02:57:28 -0500 Received: from ms-10.1blu.de ([178.254.4.101]:44746) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nKbfI-0005kM-Hi for bug-grep@gnu.org; Thu, 17 Feb 2022 02:57:27 -0500 Received: from [77.1.43.105] (helo=serenity) by ms-10.1blu.de with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nKbf5-0003EA-4k for bug-grep@gnu.org; Thu, 17 Feb 2022 08:57:11 +0100 Date: Thu, 17 Feb 2022 08:57:10 +0100 From: Ulrich Eckhardt Message-ID: <20220217085710.6aea4926@serenity> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="MP_/X8XbBpY+pP7ExYS1KnrQoWq" X-Con-Id: 144198 X-Con-U: 0-ulricheckhardt X-Originating-IP: 77.1.43.105 Received-SPF: none client-ip=178.254.4.101; envelope-from=ulrich.eckhardt@base-42.de; helo=ms-10.1blu.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -2.3 (--) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) --MP_/X8XbBpY+pP7ExYS1KnrQoWq Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline Greetings! The attached patch add a `--pipe` option to grep. When used, grep only exits with with nonzero status on error. In particular, it doesn't signal "match" / "no match" through the exit code. Here's an example using Bash: # enable automatic error handling set -eo pipefail # grep for issues in a logfile to produce a report cat logfile | grep issue | sort --unique If grep doesn't find "issue" in its input (which is not an error, obviously), it exits with status 1. Bash interprets this nonzero exit code as an error and terminates with an error itself. In order to fix that bug in the above script, you currently have to replace `grep ...` with `grep ... || [ $? = 1 ]`, which is not really readable. As alternative, I've implemented a `--pipe` option, which only returns nonzero on actual errors, but not when there is no match. This is a bit of a complementary option to `--quiet`. Open tasks here: * FSF paperwork is not finished, so obviously the patch can't be applied yet. * Should I add a `-p` to complement the long `--pipe`? * Should I call it `--pipe` at all? The other alternative I came up with was `--filter`. I don't really like either of them very much. Cheers! Uli --MP_/X8XbBpY+pP7ExYS1KnrQoWq Content-Type: text/x-patch Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=gnu-grep-pipe-option.patch >From 02f00ec50e820b9320af66ee7bb47bab36184f96 Mon Sep 17 00:00:00 2001 From: Ulrich Eckhardt Date: Mon, 14 Feb 2022 19:23:10 +0100 Subject: [PATCH 1/4] grep: implement `--pipe` option * src/grep.c: Implement `--pipe` option, which does not indicate matches or lack thereof in the exit code but only filters the input into the output. --- src/grep.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/grep.c b/src/grep.c index 9c933e4..2260d7d 100644 --- a/src/grep.c +++ b/src/grep.c @@ -498,7 +498,8 @@ enum INCLUDE_OPTION, LINE_BUFFERED_OPTION, LABEL_OPTION, - NO_IGNORE_CASE_OPTION + NO_IGNORE_CASE_OPTION, + PIPE_OPTION }; /* Long options equivalences. */ @@ -543,6 +544,7 @@ static struct option const long_options[] = {"null", no_argument, NULL, 'Z'}, {"null-data", no_argument, NULL, 'z'}, {"only-matching", no_argument, NULL, 'o'}, + {"pipe", no_argument, NULL, PIPE_OPTION}, {"quiet", no_argument, NULL, 'q'}, {"recursive", no_argument, NULL, 'r'}, {"dereference-recursive", no_argument, NULL, 'R'}, @@ -1078,6 +1080,7 @@ static enum static int out_file; static int filename_mask; /* If zero, output nulls after filenames. */ +static bool out_pipe; /* --pipe: Don't set exit status depending on match. */ static bool out_quiet; /* Suppress all normal output. */ static bool out_invert; /* Print nonmatching stuff. */ static bool out_line; /* Print line numbers. */ @@ -2017,6 +2020,7 @@ Output control:\n\ ")); printf (_("\ -o, --only-matching show only nonempty parts of lines that match\n\ + --pipe only filter, do not set exit value depending on found matches\n\ -q, --quiet, --silent suppress all normal output\n\ --binary-files=TYPE assume that binary files are TYPE;\n\ TYPE is 'binary', 'text', or 'without-match'\n\ @@ -2698,6 +2702,10 @@ main (int argc, char **argv) only_matching = true; break; + case PIPE_OPTION: + out_pipe = true; + break; + case 'q': exit_on_match = true; exit_failure = 0; @@ -3008,5 +3016,9 @@ main (int argc, char **argv) status &= grep_command_line_arg (*files++); while (*files != NULL); - return errseen ? EXIT_TROUBLE : status; + if (errseen) + return EXIT_TROUBLE; + if (out_pipe) + return EXIT_SUCCESS; + return status; } -- 2.32.0 >From e930e55fd34f02bd9959148c1d43a3fe521e2169 Mon Sep 17 00:00:00 2001 From: Ulrich Eckhardt Date: Mon, 14 Feb 2022 21:36:39 +0100 Subject: [PATCH 2/4] tests: add tests for --pipe * tests/pipe: Add new test file exercising the `--pipe` option. * tests/Makefile.am: Register new test file. --- tests/Makefile.am | 1 + tests/pipe | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100755 tests/pipe diff --git a/tests/Makefile.am b/tests/Makefile.am index a211cb6..ae8ffc8 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -148,6 +148,7 @@ TESTS = \ pcre-w \ pcre-wx-backref \ pcre-z \ + pipe \ posix-bracket \ prefix-of-multibyte \ proc \ diff --git a/tests/pipe b/tests/pipe new file mode 100755 index 0000000..9c789bb --- /dev/null +++ b/tests/pipe @@ -0,0 +1,42 @@ +#!/bin/sh +# tests for --pipe commandline option +# A missing or found match does not influence the exit status. + +. "${srcdir=.}/init.sh" +path_prepend_ ../src + +require_timeout_ + +failures=0 + +# should return 0 even though it doesn't find a match in stdin +echo -n '' | LC_ALL=C timeout 10s grep --pipe abcd +if test $? -ne 0 ; then + echo 'Status: Wrong status code, test #1 failed' + failures=1 +fi + +# should return 0 even though it doesn't find a match in the input file +echo -n '' > in +LC_ALL=C timeout 10s grep --pipe abcd in +if test $? -ne 0 ; then + echo 'Status: Wrong status code, test #2 failed' + failures=1 +fi + +# should return 0 even though it doesn't find a match in stdin +echo -n '' | LC_ALL=C timeout 10s grep --invert-match --pipe abcd +if test $? -ne 0 ; then + echo 'Status: Wrong status code, test #3 failed' + failures=1 +fi + +# should return 0 even though it doesn't find a non-match in the input file +echo -n '' > in +LC_ALL=C timeout 10s grep --invert-match --pipe abcd in +if test $? -ne 0 ; then + echo 'Status: Wrong status code, test #4 failed' + failures=1 +fi + +Exit $failures -- 2.32.0 >From 7bcc7fb748be62bf87be15e5d1f78d54b6c10823 Mon Sep 17 00:00:00 2001 From: Ulrich Eckhardt Date: Tue, 15 Feb 2022 20:46:12 +0100 Subject: [PATCH 3/4] doc: update manpage * doc/grep.in.1: Add --pipe manpage documentation. --- doc/grep.in.1 | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/doc/grep.in.1 b/doc/grep.in.1 index 372b892..89aa5fc 100644 --- a/doc/grep.in.1 +++ b/doc/grep.in.1 @@ -357,6 +357,14 @@ non-matching lines. Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line. .TP +.B \-\^\-pipe +For use in pipelines. Just apply the configured input filters but +don't set any exit status depending on found matches. Not matching +the search pattern is not always an error, so don't report it like +one. The use is shell scripting (e.g. Bash's `set -o pipefail`), +which otherwise requires explicitly checking for exit codes 0 or 1 +in the middle of a pipeline. +.TP .BR \-q ", " \-\^\-quiet ", " \-\^\-silent Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, @@ -971,7 +979,11 @@ or or .B \-\^\-silent is used and a line is selected, the exit status is 0 even if an error -occurred. +occurred. With the +.B \-\^\-pipe +option, the exit status is 2 when an error occurred and 0 otherwise. +Other @command{grep} implementations may exit with status greater than +2 on error. . .SH ENVIRONMENT The behavior of -- 2.32.0 >From 4fd80ac450dc2429dd03e803c9a8a729307ba069 Mon Sep 17 00:00:00 2001 From: Ulrich Eckhardt Date: Tue, 15 Feb 2022 20:57:12 +0100 Subject: [PATCH 4/4] doc: update texinfo * doc/grep.texi: Add --pipe documentation. --- doc/grep.texi | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/doc/grep.texi b/doc/grep.texi index 37ef839..b2a98bd 100644 --- a/doc/grep.texi +++ b/doc/grep.texi @@ -387,6 +387,16 @@ Output lines use the same delimiters as input, and delimiters are null bytes if @option{-z} (@option{--null-data}) is also used (@pxref{Other Options}). +@item --pipe +@opindex --pipe +@cindex --pipe +For use in pipelines. Just apply the configured input filters but +don't set any exit status depending on found matches. Not matching +the search pattern is not always an error, so don't report it like +one. The use is shell scripting (e.g. Bash's `set -o pipefail`), +which otherwise requires explicitly checking for exit codes 0 or 1 +in the middle of a pipeline. + @item -q @itemx --quiet @itemx --silent @@ -1093,8 +1103,9 @@ Normally the exit status is 0 if a line is selected, 1 if no lines were selected, and 2 if an error occurred. However, if the @option{-q} or @option{--quiet} or @option{--silent} option is used and a line is selected, the exit status is 0 even if an error -occurred. Other @command{grep} implementations may exit with status -greater than 2 on error. +occurred. With the @option{--pipe} option, the exit status is 2 +when an error occurred and 0 otherwise. Other @command{grep} +implementations may exit with status greater than 2 on error. @node grep Programs @section @command{grep} Programs -- 2.32.0 --MP_/X8XbBpY+pP7ExYS1KnrQoWq-- From unknown Sun Aug 17 10:18:09 2025 X-Loop: help-debbugs@gnu.org Subject: bug#54035: Patch for easier use in scripting pipelines Resent-From: Paul Eggert Original-Sender: "Debbugs-submit" Resent-CC: bug-grep@gnu.org Resent-Date: Fri, 18 Feb 2022 03:06:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 54035 X-GNU-PR-Package: grep X-GNU-PR-Keywords: To: Ulrich Eckhardt Cc: 54035@debbugs.gnu.org Received: via spool by 54035-submit@debbugs.gnu.org id=B54035.164515355510675 (code B ref 54035); Fri, 18 Feb 2022 03:06:02 +0000 Received: (at 54035) by debbugs.gnu.org; 18 Feb 2022 03:05:55 +0000 Received: from localhost ([127.0.0.1]:53080 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nKtak-0002m7-Re for submit@debbugs.gnu.org; Thu, 17 Feb 2022 22:05:55 -0500 Received: from zimbra.cs.ucla.edu ([131.179.128.68]:44368) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nKtai-0002lt-HU for 54035@debbugs.gnu.org; Thu, 17 Feb 2022 22:05:53 -0500 Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 41C23160162; Thu, 17 Feb 2022 19:05:46 -0800 (PST) Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id BSAMGTXK9woW; Thu, 17 Feb 2022 19:05:45 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 82FEC16016B; Thu, 17 Feb 2022 19:05:45 -0800 (PST) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id de_pNEDAYgbC; Thu, 17 Feb 2022 19:05:45 -0800 (PST) Received: from [192.168.1.9] (cpe-172-91-119-151.socal.res.rr.com [172.91.119.151]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id 599D3160162; Thu, 17 Feb 2022 19:05:45 -0800 (PST) Message-ID: <341cf22a-b6ee-5efb-9617-00b0ef993990@cs.ucla.edu> Date: Thu, 17 Feb 2022 19:05:45 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Content-Language: en-US References: <20220217085710.6aea4926@serenity> From: Paul Eggert Organization: UCLA Computer Science Department In-Reply-To: <20220217085710.6aea4926@serenity> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Score: -2.3 (--) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) On 2/16/22 23:57, Ulrich Eckhardt wrote: > In order to fix that bug in the above script, you currently have to > replace `grep ...` with `grep ... || [ $? = 1 ]`, which is not really > readable. Actually, appending something "|| test $? -eq 1" looks readable to me; plus, it already works and is portable to non-GNU systems which is a plus. Furthermore, it also works with other programs that also return 0,1,>1 depending on success,failure,error (e.g., 'cmp', 'diff', 'sort'), and it doesn't sound like much of a win to add unportable --pipe options to every such program. And it's not just commands like 'cmp' and 'grep'. The following causes Bash to exit on GNU/Linux: set -eo pipefail cat /usr/share/dict/american-english | grep -l '^' This is not because of anything 'grep' does, as 'grep' exits with status zero. It's because 'cat' exits with nonzero status. Surely we shouldn't add a --pipe option to 'cat' too. Scripts that use "set -eo pipefail" need to be verrrry careful regardless of what we do with 'grep'; and if they are careful it won't help much to add a --pipe option to 'grep'. From unknown Sun Aug 17 10:18:09 2025 X-Loop: help-debbugs@gnu.org Subject: bug#54035: Patch for easier use in scripting pipelines Resent-From: Ulrich Eckhardt Original-Sender: "Debbugs-submit" Resent-CC: bug-grep@gnu.org Resent-Date: Fri, 18 Feb 2022 08:28:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 54035 X-GNU-PR-Package: grep X-GNU-PR-Keywords: To: Paul Eggert Cc: 54035@debbugs.gnu.org Received: via spool by 54035-submit@debbugs.gnu.org id=B54035.164517284010501 (code B ref 54035); Fri, 18 Feb 2022 08:28:02 +0000 Received: (at 54035) by debbugs.gnu.org; 18 Feb 2022 08:27:20 +0000 Received: from localhost ([127.0.0.1]:53401 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nKybo-0002jI-EE for submit@debbugs.gnu.org; Fri, 18 Feb 2022 03:27:20 -0500 Received: from ms-10.1blu.de ([178.254.4.101]:51562) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nKybm-0002j9-Lj for 54035@debbugs.gnu.org; Fri, 18 Feb 2022 03:27:19 -0500 Received: from [95.112.224.92] (helo=serenity) by ms-10.1blu.de with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nKybk-00079k-8l; Fri, 18 Feb 2022 09:27:16 +0100 Date: Fri, 18 Feb 2022 09:27:15 +0100 From: Ulrich Eckhardt Message-ID: <20220218092715.63f4341f@serenity> In-Reply-To: <341cf22a-b6ee-5efb-9617-00b0ef993990@cs.ucla.edu> References: <20220217085710.6aea4926@serenity> <341cf22a-b6ee-5efb-9617-00b0ef993990@cs.ucla.edu> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Con-Id: 144198 X-Con-U: 0-ulricheckhardt X-Originating-IP: 95.112.224.92 X-Spam-Score: -0.0 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) On Thu, 17 Feb 2022 19:05:45 -0800 Paul Eggert wrote: > On 2/16/22 23:57, Ulrich Eckhardt wrote: > > In order to fix that bug in the above script, you currently have to > > replace `grep ...` with `grep ... || [ $? = 1 ]`, which is not > > really readable. > > Actually, appending something "|| test $? -eq 1" looks readable to > me; plus, it already works and is portable to non-GNU systems which > is a plus. Furthermore, it also works with other programs that also > return 0,1,>1 depending on success,failure,error (e.g., 'cmp', > 'diff', 'sort'), and it doesn't sound like much of a win to add > unportable --pipe options to every such program. I wasn't aware of those, but I'd say they are candidates as well! I receive the result of the grep operation on stdout, so I don't want any categorization via the exit code. I'm also still not 100% sure on the idea, which is why I'm putting this up for discussion. I think my main two arguments are - Shell scripting uses nonzero exit codes to signal errors by default. grep is an exception here (as are cmp, diff and sort as I understand you), and for a reason, but it doesn't provide an option to use default behaviour. - Also, just arguing in the context of grep, there is an option to "just tell me if there was a match, don't give me the results" and I want a complementary "just give me the results, don't tell me if there was a match". > And it's not just commands like 'cmp' and 'grep'. The following > causes Bash to exit on GNU/Linux: > > set -eo pipefail > cat /usr/share/dict/american-english | grep -l '^' > > This is not because of anything 'grep' does, as 'grep' exits with > status zero. It's because 'cat' exits with nonzero status. Surely we > shouldn't add a --pipe option to 'cat' too. It doesn't do that here, I wonder why you are seeing an error there? Also, if it did, that would signal an actual error, which is behaviour I'm completely fine with. That said, I received feedback from another side (busybox) which is strongly against a `-p` alias, because BSD grep already uses that one. Thank you for your input, Paul, I'm enjoying this exchange! Uli From unknown Sun Aug 17 10:18:09 2025 X-Loop: help-debbugs@gnu.org Subject: bug#54035: Patch for easier use in scripting pipelines Resent-From: Paul Eggert Original-Sender: "Debbugs-submit" Resent-CC: bug-grep@gnu.org Resent-Date: Sat, 19 Feb 2022 16:02:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 54035 X-GNU-PR-Package: grep X-GNU-PR-Keywords: To: Ulrich Eckhardt Cc: 54035@debbugs.gnu.org Received: via spool by 54035-submit@debbugs.gnu.org id=B54035.16452864914172 (code B ref 54035); Sat, 19 Feb 2022 16:02:02 +0000 Received: (at 54035) by debbugs.gnu.org; 19 Feb 2022 16:01:31 +0000 Received: from localhost ([127.0.0.1]:58614 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nLSAt-00015D-By for submit@debbugs.gnu.org; Sat, 19 Feb 2022 11:01:31 -0500 Received: from zimbra.cs.ucla.edu ([131.179.128.68]:45508) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nLSAq-00014u-Mm for 54035@debbugs.gnu.org; Sat, 19 Feb 2022 11:01:29 -0500 Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 4EA0C1601B2; Sat, 19 Feb 2022 08:01:22 -0800 (PST) Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id YMxMuawSj3ky; Sat, 19 Feb 2022 08:01:21 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 67A591601B6; Sat, 19 Feb 2022 08:01:21 -0800 (PST) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id Inw0WrIcdyr4; Sat, 19 Feb 2022 08:01:21 -0800 (PST) Received: from [192.168.1.9] (cpe-172-91-119-151.socal.res.rr.com [172.91.119.151]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id 36DF91601B2; Sat, 19 Feb 2022 08:01:21 -0800 (PST) Message-ID: Date: Sat, 19 Feb 2022 08:01:20 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Content-Language: en-US References: <20220217085710.6aea4926@serenity> <341cf22a-b6ee-5efb-9617-00b0ef993990@cs.ucla.edu> <20220218092715.63f4341f@serenity> From: Paul Eggert Organization: UCLA Computer Science Department In-Reply-To: <20220218092715.63f4341f@serenity> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Score: -2.3 (--) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) On 2/18/22 00:27, Ulrich Eckhardt wrote: > - Shell scripting uses nonzero exit codes to signal errors by default. > grep is an exception here (as are cmp, diff and sort as I understand > you), I'm sure there are other exceptions. And there's a long tradition for these exceptions. It doesn't sound realistic to upend this longstanding practice, or to add options to every such program. > - Also, just arguing in the context of grep, there is an option to > "just tell me if there was a match, don't give me the results" That's for efficiency; grep can be waaay faster with -q. There is no efficiency argument for the changes you're proposing. >> The following >> causes Bash to exit on GNU/Linux: >> >> set -eo pipefail >> cat /usr/share/dict/american-english | grep -l '^' >> >> This is not because of anything 'grep' does, as 'grep' exits with >> status zero. It's because 'cat' exits with nonzero status. Surely we >> shouldn't add a --pipe option to 'cat' too. > > It doesn't do that here, I wonder why you are seeing an error there? Possibly you're using a GNU/Linux variant where the dictionary is located elsewhere? Or you have something in your .profile? I'm running Ubuntu 21.10 x86-64, and if I run this shell command: bash --norc --noprofile -c 'set -eo pipefail; cat /usr/share/dict/american-english | grep -l "^"; echo done' the output is: (standard input) which means that Bash exited without doing the 'echo done'. > Also, if it did, that would signal an actual error, which is behaviour > I'm completely fine with. There is no actual error, in that the "cat ... | grep ..." command does just what I wanted it to: grep reported that standard input contained a match (which it did). This may help to explain my previous remark that scripts that use "set -eo pipefail" need to be verrrry careful. From unknown Sun Aug 17 10:18:09 2025 X-Loop: help-debbugs@gnu.org Subject: bug#54035: Patch for easier use in scripting pipelines Resent-From: Ulrich Eckhardt Original-Sender: "Debbugs-submit" Resent-CC: bug-grep@gnu.org Resent-Date: Thu, 24 Feb 2022 07:12:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 54035 X-GNU-PR-Package: grep X-GNU-PR-Keywords: To: Paul Eggert Cc: 54035@debbugs.gnu.org Received: via spool by 54035-submit@debbugs.gnu.org id=B54035.164568670424508 (code B ref 54035); Thu, 24 Feb 2022 07:12:01 +0000 Received: (at 54035) by debbugs.gnu.org; 24 Feb 2022 07:11:44 +0000 Received: from localhost ([127.0.0.1]:47375 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nN8Hw-0006NE-74 for submit@debbugs.gnu.org; Thu, 24 Feb 2022 02:11:44 -0500 Received: from ms-10.1blu.de ([178.254.4.101]:49366) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nN8Hu-0006N5-2h for 54035@debbugs.gnu.org; Thu, 24 Feb 2022 02:11:43 -0500 Received: from [77.8.120.226] (helo=serenity) by ms-10.1blu.de with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nN8Hr-0007Nq-OQ; Thu, 24 Feb 2022 08:11:39 +0100 Date: Thu, 24 Feb 2022 08:11:37 +0100 From: Ulrich Eckhardt Message-ID: <20220224081137.58bf1723@serenity> In-Reply-To: References: <20220217085710.6aea4926@serenity> <341cf22a-b6ee-5efb-9617-00b0ef993990@cs.ucla.edu> <20220218092715.63f4341f@serenity> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Con-Id: 144198 X-Con-U: 0-ulricheckhardt X-Originating-IP: 77.8.120.226 X-Spam-Score: -0.0 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) On Sat, 19 Feb 2022 08:01:20 -0800 Paul Eggert wrote: > bash --norc --noprofile -c 'set -eo pipefail; cat > /usr/share/dict/american-english | grep -l "^"; echo done' > > the output is: > > (standard input) > > which means that Bash exited without doing the 'echo done'. I'm running fish as shell, and that doesn't do that. Using bash, it behaves as you describe. Just for my understanding, grep stops reading when it finds the first match and then the shell closes the output stream of cat. That in turn causes cat to fail (exit code 141, meaning SIGPIPE), because it can't write the rest of the data that it wants, right? In any case, you have delivered some convincing arguments. I'll turn my attention on the manpage instead. The benchmark (if that's what you want to call it) that brought me to this was that grep behaviour confused me and that I couldn't find anything even from reading the docs. I think that short reads (which could cause SIGPIPE) and the non-error exit code 1 deserve mention there. I'll take a look and perhaps file another patch. So long... Uli From unknown Sun Aug 17 10:18:09 2025 MIME-Version: 1.0 X-Mailer: MIME-tools 5.505 (Entity 5.505) X-Loop: help-debbugs@gnu.org From: help-debbugs@gnu.org (GNU bug Tracking System) To: Ulrich Eckhardt Subject: bug#54035: closed (Re: bug#54035: Patch for easier use in scripting pipelines) Message-ID: References: <7c398c12-dba7-88f9-9d06-aa15b1d3a48e@cs.ucla.edu> <20220217085710.6aea4926@serenity> X-Gnu-PR-Message: they-closed 54035 X-Gnu-PR-Package: grep Reply-To: 54035@debbugs.gnu.org Date: Thu, 24 Feb 2022 19:10:02 +0000 Content-Type: multipart/mixed; boundary="----------=_1645729802-3180-1" This is a multi-part message in MIME format... ------------=_1645729802-3180-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Your bug report #54035: Patch for easier use in scripting pipelines which was filed against the grep package, has been closed. The explanation is attached below, along with your original report. If you require more details, please reply to 54035@debbugs.gnu.org. --=20 54035: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=3D54035 GNU Bug Tracking System Contact help-debbugs@gnu.org with problems ------------=_1645729802-3180-1 Content-Type: message/rfc822 Content-Disposition: inline Content-Transfer-Encoding: 7bit Received: (at 54035-done) by debbugs.gnu.org; 24 Feb 2022 19:09:15 +0000 Received: from localhost ([127.0.0.1]:50432 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nNJUJ-0000oC-BF for submit@debbugs.gnu.org; Thu, 24 Feb 2022 14:09:15 -0500 Received: from zimbra.cs.ucla.edu ([131.179.128.68]:43340) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nNJUH-0000ny-4s for 54035-done@debbugs.gnu.org; Thu, 24 Feb 2022 14:09:14 -0500 Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 29E1616016A; Thu, 24 Feb 2022 11:09:07 -0800 (PST) Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id cSrJuOmkMYwJ; Thu, 24 Feb 2022 11:09:06 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 3B2E916016B; Thu, 24 Feb 2022 11:09:06 -0800 (PST) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 9OJ3jJW-1UHD; Thu, 24 Feb 2022 11:09:06 -0800 (PST) Received: from [192.168.1.9] (cpe-172-91-119-151.socal.res.rr.com [172.91.119.151]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id 0DE9B16016A; Thu, 24 Feb 2022 11:09:06 -0800 (PST) Content-Type: multipart/mixed; boundary="------------ABwlpJFdcpTwsF2OKscEDAz6" Message-ID: <7c398c12-dba7-88f9-9d06-aa15b1d3a48e@cs.ucla.edu> Date: Thu, 24 Feb 2022 11:09:05 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Content-Language: en-US To: Ulrich Eckhardt References: <20220217085710.6aea4926@serenity> <341cf22a-b6ee-5efb-9617-00b0ef993990@cs.ucla.edu> <20220218092715.63f4341f@serenity> <20220224081137.58bf1723@serenity> From: Paul Eggert Organization: UCLA Computer Science Department Subject: Re: bug#54035: Patch for easier use in scripting pipelines In-Reply-To: <20220224081137.58bf1723@serenity> X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 54035-done Cc: 54035-done@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) This is a multi-part message in MIME format. --------------ABwlpJFdcpTwsF2OKscEDAz6 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2/23/22 23:11, Ulrich Eckhardt wrote: > Just for my understanding, grep stops reading > when it finds the first match and then the shell closes the output > stream of cat. That in turn causes cat to fail (exit code 141, meaning > SIGPIPE), because it can't write the rest of the data that it wants, > right? Right. > I think that short reads (which could cause SIGPIPE) and the > non-error exit code 1 deserve mention there. I'll take a look and > perhaps file another patch. I installed the attached to try to document this better. --------------ABwlpJFdcpTwsF2OKscEDAz6 Content-Type: text/x-patch; charset=UTF-8; name="0001-doc-mention-issues-with-set-e.patch" Content-Disposition: attachment; filename="0001-doc-mention-issues-with-set-e.patch" Content-Transfer-Encoding: base64 RnJvbSA4OWRlMjFiZDI1MjUwODhjZjVjZTEyMzk1NTE1Y2ExZDhlZjU4MmEyIE1vbiBTZXAg MTcgMDA6MDA6MDAgMjAwMQpGcm9tOiBQYXVsIEVnZ2VydCA8ZWdnZXJ0QGNzLnVjbGEuZWR1 PgpEYXRlOiBUaHUsIDI0IEZlYiAyMDIyIDExOjA3OjE0IC0wODAwClN1YmplY3Q6IFtQQVRD SF0gZG9jOiBtZW50aW9uIGlzc3VlcyB3aXRoIHNldCAtZQoKKiBkb2MvZ3JlcC50ZXhpIChV c2FnZSwgUGVyZm9ybWFuY2UpOiBNZW50aW9uIGVhcmx5IGV4aXRzIChCdWcjNTQwMzUpLgot LS0KIGRvYy9ncmVwLnRleGkgfCAyNCArKysrKysrKysrKysrKysrKysrKysrKysKIDEgZmls ZSBjaGFuZ2VkLCAyNCBpbnNlcnRpb25zKCspCgpkaWZmIC0tZ2l0IGEvZG9jL2dyZXAudGV4 aSBiL2RvYy9ncmVwLnRleGkKaW5kZXggMzdlZjgzOS4uZWJiZWZkYSAxMDA2NDQKLS0tIGEv ZG9jL2dyZXAudGV4aQorKysgYi9kb2MvZ3JlcC50ZXhpCkBAIC0xODYyLDYgKzE4NjIsMjIg QEAgVXNlIHRoZSBzcGVjaWFsIGZpbGUgbmFtZSBAc2FtcHstfToKIGNhdCAvZXRjL3Bhc3N3 ZCB8IGdyZXAgJ2FsYWluJyAtIC9ldGMvbW90ZAogQGVuZCBleGFtcGxlCiAKK0BpdGVtCitX aHkgY2FuJ3QgSSBjb21iaW5lIHRoZSBzaGVsbCdzIEBzYW1we3NldCAtZX0gd2l0aCBAY29t bWFuZHtncmVwfT8KKworVGhlIEBjb21tYW5ke2dyZXB9IGNvbW1hbmQgZm9sbG93cyB0aGUg Y29udmVudGlvbiBvZiBwcm9ncmFtcyBsaWtlCitAY29tbWFuZHtjbXB9IGFuZCBAY29tbWFu ZHtkaWZmfSB3aGVyZSBhbiBleGl0IHN0YXR1cyBvZiAxIGlzIG5vdCBhbgorZXJyb3IuICBU aGUgc2hlbGwgY29tbWFuZCBAc2FtcHtzZXQgLWV9IGNhdXNlcyB0aGUgc2hlbGwgdG8gZXhp dCBpZgorYW55IHN1YmNvbW1hbmQgZXhpdHMgd2l0aCBub256ZXJvIHN0YXR1cywgYW5kIHRo aXMgd2lsbCBjYXVzZSB0aGUKK3NoZWxsIHRvIGV4aXQgbWVyZWx5IGJlY2F1c2UgQGNvbW1h bmR7Z3JlcH0gc2VsZWN0ZWQgbm8gbGluZXMsCit3aGljaCBpcyBvcmRpbmFyaWx5IG5vdCB3 aGF0IHlvdSB3YW50LgorCitUaGVyZSBpcyBhIHJlbGF0ZWQgcHJvYmxlbSB3aXRoIEJhc2gn cyBAY29tbWFuZHtzZXQgLWUgLW8gcGlwZWZhaWx9LgorU2luY2UgQGNvbW1hbmR7Z3JlcH0g ZG9lcyBub3QgYWx3YXlzIHJlYWQgYWxsIGl0cyBpbnB1dCwgYSBjb21tYW5kCitvdXRwdXR0 aW5nIHRvIGEgcGlwZSByZWFkIGJ5IEBjb21tYW5ke2dyZXB9IGNhbiBmYWlsIHdoZW4KK0Bj b21tYW5ke2dyZXB9IGV4aXRzIGJlZm9yZSByZWFkaW5nIGFsbCBpdHMgaW5wdXQsIGFuZCB0 aGUgY29tbWFuZCdzCitmYWlsdXJlIGNhbiBjYXVzZSBCYXNoIHRvIGV4aXQuCisKIEBpdGVt CiBXaHkgaXMgdGhpcyBiYWNrLXJlZmVyZW5jZSBmYWlsaW5nPwogCkBAIC0xOTk4LDYgKzIw MTQsMTQgQEAgbmVlZGluZyB0byByZWFkIHRoZSB6ZXJvcy4gIFRoaXMgb3B0aW1pemF0aW9u IGlzIG5vdCBhdmFpbGFibGUgaWYgdGhlCiBEaXJlY3RvcnkgU2VsZWN0aW9ufSksIHVubGVz cyB0aGUgQG9wdGlvbnsten0gKEBvcHRpb257LS1udWxsLWRhdGF9KQogb3B0aW9uIGlzIGFs c28gdXNlZCAoQHB4cmVme090aGVyIE9wdGlvbnN9KS4KIAorQGNpbmRleCBwaXBlbGluZXMg YW5kIHJlYWRpbmcKK0ZvciBlZmZpY2llbmN5IEBjb21tYW5ke2dyZXB9IGRvZXMgbm90IGFs d2F5cyByZWFkIGFsbCBpdHMgaW5wdXQuCitGb3IgZXhhbXBsZSwgdGhlIHNoZWxsIGNvbW1h bmQgQHNhbXB7c2VkICcvXi4uLiQvZCcgfCBncmVwIC1xIFh9IGNhbgorY2F1c2UgQGNvbW1h bmR7Z3JlcH0gdG8gZXhpdCBpbW1lZGlhdGVseSBhZnRlciByZWFkaW5nIGEgbGluZQorY29u dGFpbmluZyBAc2FtcHtYfSwgd2l0aG91dCBib3RoZXJpbmcgdG8gcmVhZCB0aGUgcmVzdCBv ZiBpdHMgaW5wdXQgZGF0YS4KK1RoaXMgaW4gdHVybiBjYW4gY2F1c2UgQGNvbW1hbmR7c2Vk fSB0byBleGl0IHdpdGggYSBub256ZXJvIHN0YXR1cyBiZWNhdXNlCitAY29tbWFuZHtzZWR9 IGNhbm5vdCB3cml0ZSB0byBpdHMgb3V0cHV0IHBpcGUgYWZ0ZXIgQGNvbW1hbmR7Z3JlcH0g ZXhpdHMuCisKIEZvciBtb3JlIGFib3V0IHRoZSBhbGdvcml0aG1zIHVzZWQgYnkgQGNvbW1h bmR7Z3JlcH0gYW5kIGFib3V0CiByZWxhdGVkIHN0cmluZyBtYXRjaGluZyBhbGdvcml0aG1z LCBzZWU6CiAKLS0gCjIuMzIuMAoK --------------ABwlpJFdcpTwsF2OKscEDAz6-- ------------=_1645729802-3180-1 Content-Type: message/rfc822 Content-Disposition: inline Content-Transfer-Encoding: 7bit Received: (at submit) by debbugs.gnu.org; 17 Feb 2022 07:57:31 +0000 Received: from localhost ([127.0.0.1]:50016 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nKbfO-0007jc-Ho for submit@debbugs.gnu.org; Thu, 17 Feb 2022 02:57:31 -0500 Received: from lists.gnu.org ([209.51.188.17]:47822) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nKbfM-0007jU-F9 for submit@debbugs.gnu.org; Thu, 17 Feb 2022 02:57:29 -0500 Received: from eggs.gnu.org ([209.51.188.92]:44252) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nKbfM-0004wq-1o for bug-grep@gnu.org; Thu, 17 Feb 2022 02:57:28 -0500 Received: from ms-10.1blu.de ([178.254.4.101]:44746) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nKbfI-0005kM-Hi for bug-grep@gnu.org; Thu, 17 Feb 2022 02:57:27 -0500 Received: from [77.1.43.105] (helo=serenity) by ms-10.1blu.de with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nKbf5-0003EA-4k for bug-grep@gnu.org; Thu, 17 Feb 2022 08:57:11 +0100 Date: Thu, 17 Feb 2022 08:57:10 +0100 From: Ulrich Eckhardt To: bug-grep@gnu.org Subject: Patch for easier use in scripting pipelines Message-ID: <20220217085710.6aea4926@serenity> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="MP_/X8XbBpY+pP7ExYS1KnrQoWq" X-Con-Id: 144198 X-Con-U: 0-ulricheckhardt X-Originating-IP: 77.1.43.105 Received-SPF: none client-ip=178.254.4.101; envelope-from=ulrich.eckhardt@base-42.de; helo=ms-10.1blu.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: submit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) --MP_/X8XbBpY+pP7ExYS1KnrQoWq Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline Greetings! The attached patch add a `--pipe` option to grep. When used, grep only exits with with nonzero status on error. In particular, it doesn't signal "match" / "no match" through the exit code. Here's an example using Bash: # enable automatic error handling set -eo pipefail # grep for issues in a logfile to produce a report cat logfile | grep issue | sort --unique If grep doesn't find "issue" in its input (which is not an error, obviously), it exits with status 1. Bash interprets this nonzero exit code as an error and terminates with an error itself. In order to fix that bug in the above script, you currently have to replace `grep ...` with `grep ... || [ $? = 1 ]`, which is not really readable. As alternative, I've implemented a `--pipe` option, which only returns nonzero on actual errors, but not when there is no match. This is a bit of a complementary option to `--quiet`. Open tasks here: * FSF paperwork is not finished, so obviously the patch can't be applied yet. * Should I add a `-p` to complement the long `--pipe`? * Should I call it `--pipe` at all? The other alternative I came up with was `--filter`. I don't really like either of them very much. Cheers! Uli --MP_/X8XbBpY+pP7ExYS1KnrQoWq Content-Type: text/x-patch Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=gnu-grep-pipe-option.patch >From 02f00ec50e820b9320af66ee7bb47bab36184f96 Mon Sep 17 00:00:00 2001 From: Ulrich Eckhardt Date: Mon, 14 Feb 2022 19:23:10 +0100 Subject: [PATCH 1/4] grep: implement `--pipe` option * src/grep.c: Implement `--pipe` option, which does not indicate matches or lack thereof in the exit code but only filters the input into the output. --- src/grep.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/grep.c b/src/grep.c index 9c933e4..2260d7d 100644 --- a/src/grep.c +++ b/src/grep.c @@ -498,7 +498,8 @@ enum INCLUDE_OPTION, LINE_BUFFERED_OPTION, LABEL_OPTION, - NO_IGNORE_CASE_OPTION + NO_IGNORE_CASE_OPTION, + PIPE_OPTION }; /* Long options equivalences. */ @@ -543,6 +544,7 @@ static struct option const long_options[] = {"null", no_argument, NULL, 'Z'}, {"null-data", no_argument, NULL, 'z'}, {"only-matching", no_argument, NULL, 'o'}, + {"pipe", no_argument, NULL, PIPE_OPTION}, {"quiet", no_argument, NULL, 'q'}, {"recursive", no_argument, NULL, 'r'}, {"dereference-recursive", no_argument, NULL, 'R'}, @@ -1078,6 +1080,7 @@ static enum static int out_file; static int filename_mask; /* If zero, output nulls after filenames. */ +static bool out_pipe; /* --pipe: Don't set exit status depending on match. */ static bool out_quiet; /* Suppress all normal output. */ static bool out_invert; /* Print nonmatching stuff. */ static bool out_line; /* Print line numbers. */ @@ -2017,6 +2020,7 @@ Output control:\n\ ")); printf (_("\ -o, --only-matching show only nonempty parts of lines that match\n\ + --pipe only filter, do not set exit value depending on found matches\n\ -q, --quiet, --silent suppress all normal output\n\ --binary-files=TYPE assume that binary files are TYPE;\n\ TYPE is 'binary', 'text', or 'without-match'\n\ @@ -2698,6 +2702,10 @@ main (int argc, char **argv) only_matching = true; break; + case PIPE_OPTION: + out_pipe = true; + break; + case 'q': exit_on_match = true; exit_failure = 0; @@ -3008,5 +3016,9 @@ main (int argc, char **argv) status &= grep_command_line_arg (*files++); while (*files != NULL); - return errseen ? EXIT_TROUBLE : status; + if (errseen) + return EXIT_TROUBLE; + if (out_pipe) + return EXIT_SUCCESS; + return status; } -- 2.32.0 >From e930e55fd34f02bd9959148c1d43a3fe521e2169 Mon Sep 17 00:00:00 2001 From: Ulrich Eckhardt Date: Mon, 14 Feb 2022 21:36:39 +0100 Subject: [PATCH 2/4] tests: add tests for --pipe * tests/pipe: Add new test file exercising the `--pipe` option. * tests/Makefile.am: Register new test file. --- tests/Makefile.am | 1 + tests/pipe | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100755 tests/pipe diff --git a/tests/Makefile.am b/tests/Makefile.am index a211cb6..ae8ffc8 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -148,6 +148,7 @@ TESTS = \ pcre-w \ pcre-wx-backref \ pcre-z \ + pipe \ posix-bracket \ prefix-of-multibyte \ proc \ diff --git a/tests/pipe b/tests/pipe new file mode 100755 index 0000000..9c789bb --- /dev/null +++ b/tests/pipe @@ -0,0 +1,42 @@ +#!/bin/sh +# tests for --pipe commandline option +# A missing or found match does not influence the exit status. + +. "${srcdir=.}/init.sh" +path_prepend_ ../src + +require_timeout_ + +failures=0 + +# should return 0 even though it doesn't find a match in stdin +echo -n '' | LC_ALL=C timeout 10s grep --pipe abcd +if test $? -ne 0 ; then + echo 'Status: Wrong status code, test #1 failed' + failures=1 +fi + +# should return 0 even though it doesn't find a match in the input file +echo -n '' > in +LC_ALL=C timeout 10s grep --pipe abcd in +if test $? -ne 0 ; then + echo 'Status: Wrong status code, test #2 failed' + failures=1 +fi + +# should return 0 even though it doesn't find a match in stdin +echo -n '' | LC_ALL=C timeout 10s grep --invert-match --pipe abcd +if test $? -ne 0 ; then + echo 'Status: Wrong status code, test #3 failed' + failures=1 +fi + +# should return 0 even though it doesn't find a non-match in the input file +echo -n '' > in +LC_ALL=C timeout 10s grep --invert-match --pipe abcd in +if test $? -ne 0 ; then + echo 'Status: Wrong status code, test #4 failed' + failures=1 +fi + +Exit $failures -- 2.32.0 >From 7bcc7fb748be62bf87be15e5d1f78d54b6c10823 Mon Sep 17 00:00:00 2001 From: Ulrich Eckhardt Date: Tue, 15 Feb 2022 20:46:12 +0100 Subject: [PATCH 3/4] doc: update manpage * doc/grep.in.1: Add --pipe manpage documentation. --- doc/grep.in.1 | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/doc/grep.in.1 b/doc/grep.in.1 index 372b892..89aa5fc 100644 --- a/doc/grep.in.1 +++ b/doc/grep.in.1 @@ -357,6 +357,14 @@ non-matching lines. Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line. .TP +.B \-\^\-pipe +For use in pipelines. Just apply the configured input filters but +don't set any exit status depending on found matches. Not matching +the search pattern is not always an error, so don't report it like +one. The use is shell scripting (e.g. Bash's `set -o pipefail`), +which otherwise requires explicitly checking for exit codes 0 or 1 +in the middle of a pipeline. +.TP .BR \-q ", " \-\^\-quiet ", " \-\^\-silent Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, @@ -971,7 +979,11 @@ or or .B \-\^\-silent is used and a line is selected, the exit status is 0 even if an error -occurred. +occurred. With the +.B \-\^\-pipe +option, the exit status is 2 when an error occurred and 0 otherwise. +Other @command{grep} implementations may exit with status greater than +2 on error. . .SH ENVIRONMENT The behavior of -- 2.32.0 >From 4fd80ac450dc2429dd03e803c9a8a729307ba069 Mon Sep 17 00:00:00 2001 From: Ulrich Eckhardt Date: Tue, 15 Feb 2022 20:57:12 +0100 Subject: [PATCH 4/4] doc: update texinfo * doc/grep.texi: Add --pipe documentation. --- doc/grep.texi | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/doc/grep.texi b/doc/grep.texi index 37ef839..b2a98bd 100644 --- a/doc/grep.texi +++ b/doc/grep.texi @@ -387,6 +387,16 @@ Output lines use the same delimiters as input, and delimiters are null bytes if @option{-z} (@option{--null-data}) is also used (@pxref{Other Options}). +@item --pipe +@opindex --pipe +@cindex --pipe +For use in pipelines. Just apply the configured input filters but +don't set any exit status depending on found matches. Not matching +the search pattern is not always an error, so don't report it like +one. The use is shell scripting (e.g. Bash's `set -o pipefail`), +which otherwise requires explicitly checking for exit codes 0 or 1 +in the middle of a pipeline. + @item -q @itemx --quiet @itemx --silent @@ -1093,8 +1103,9 @@ Normally the exit status is 0 if a line is selected, 1 if no lines were selected, and 2 if an error occurred. However, if the @option{-q} or @option{--quiet} or @option{--silent} option is used and a line is selected, the exit status is 0 even if an error -occurred. Other @command{grep} implementations may exit with status -greater than 2 on error. +occurred. With the @option{--pipe} option, the exit status is 2 +when an error occurred and 0 otherwise. Other @command{grep} +implementations may exit with status greater than 2 on error. @node grep Programs @section @command{grep} Programs -- 2.32.0 --MP_/X8XbBpY+pP7ExYS1KnrQoWq-- ------------=_1645729802-3180-1--