[arch-projects] [dbscripts] [PATCH 4/8] Use `grep &>/dev/null` instead of `grep -q` when operating on piped stdin.
Luke Shumaker
lukeshu at lukeshu.com
Wed Mar 14 04:59:10 UTC 2018
On Wed, 14 Mar 2018 00:11:12 -0400,
Eli Schwartz wrote:
>
> On 03/13/2018 09:52 PM, Luke Shumaker wrote:
> > From: Luke Shumaker <lukeshu at parabola.nu>
> >
> > `grep -q` may exit as soon as it finds a match; this is a good optimization
> > for when the input is a file. However, if the input is the output of
> > another program, then that other program will receive SIGPIPE, and further
> > writes will fail. When this happens, it might (bsdtar does) print a
> > message about a "write error" to stderr. Which is going to confuse and
> > alarm the user.
> >
> > In one of the cases, this had already been mitigated by wrapping
> > bsdtar in "echo "$(bsdtar ...)", as Bash builtin echo doesn't complain
> > if it gets SIGPIPE. However, that means we're storing the entire
> > output of bsdtar in memory, which is silly.
> > ---
> > db-functions | 2 +-
> > test/lib/common.bash | 4 ++--
> > 2 files changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/db-functions b/db-functions
> > index 58b753a..ee390ff 100644
> > --- a/db-functions
> > +++ b/db-functions
> > @@ -303,7 +303,7 @@ check_pkgfile() {
> >
> > in_array "${pkgarch}" "${ARCHES[@]}" 'any' || return 1
> >
> > - if echo "${pkgfile##*/}" | grep -q "^${pkgname}-${pkgver}-${pkgarch}"; then
> > + if echo "${pkgfile##*/}" | grep "^${pkgname}-${pkgver}-${pkgarch}" &>/dev/null; then
>
> But echo should be fine anyway?
Yeah, in this case it's for consistency with the others. It's easier
to remember "don't use `grep -q` on other commands' stdout" that it is
to work out when it's ok and when it isn't.
> Regardless this could be so much more elegant.
>
> if [[ $pkgfile = $pkgname-$pkgver-$pkgrel-$arch* ]]; then
You're right, that is better.
--
Happy hacking,
~ Luke Shumaker
More information about the arch-projects
mailing list