Re: [PATCH] Optimize usage of grep by passing -q
- From
Stefan Beller <sbeller@google.com>
- Date
- Nov 17, 2015, 01:04 UTC
- Message-ID
- <CAGZ79kYCNdDV2cDZSiugx6EuY_0BEgGMJJVm8EBWqPEE77sqbw@mail.gmail.com>
- In-Reply-To
- <CAHYJk3S-pBQGDk_PPXw5mgzuXhdKqifRB4Vwz1Nu0TecXz4JQg@mail.gmail.com>
+cc Andrey Rybak, who I credit for finding the reasoning below (he sent to me privately, without cc'ing the list)
On Mon, Nov 16, 2015 at 4:59 PM, Mikael Magnusson <mikachu@gmail.com> wrote:
Show 14 quoted lines
> On Mon, Nov 16, 2015 at 10:43 PM, Stefan Beller <sbeller@google.com> wrote: >> Instead of redirecting all grep output to /dev/null, we can just >> pass in -q instead. This preserves the exit code behavior, but is faster. >> As grep returns true if it finds at least one match, grep can exit promptly >> after finding the first line and doesn't need to find more occurrences >> which would be redirected to /dev/null anyways. >> >> This is true for the gnu version of grep. I am not sure if all >> versions of grep support this optimization. In case it is not, >> we'd revert this patch. > > POSIX specifies -q, so you should be fine. > http://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html >
>From http://www.gnu.org/software/grep/manual/grep.html :
-q --quiet --silent Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option. (-q is specified by POSIX.) -s --no-messages Suppress error messages about nonexistent or unreadable files. Portability note: unlike GNU grep, 7th Edition Unix grep did not conform to POSIX, because it lacked -q and its -s option behaved like GNU grep's -q option.1 USG-style grep also lacked -q but its -s option behaved like GNU grep's. Portable shell scripts should avoid both -q and -s and should redirect standard and error output to /dev/null instead. (-s is specified by POSIX.)
Reading that in full, I think my patch is a bad idea.