Skip to content
  • Luke Shumaker's avatar
    Don't use `grep -q` when operating on piped stdin · 23c2b82c
    Luke Shumaker authored and Eli Schwartz's avatar Eli Schwartz committed
    (By default, prefer `grep &>/dev/null`)
    
    `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 (in common.bash, in the test suite), 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.  Additionally, the way it was implemented is also wrong;
    because it was being used with `grep -qv` instead of just `grep -q`,
    it *always* found a non-matching line (even something inconsequential
    like `%NAME%`), and *never* triggered a test failure.
    
    Looking at a few of these cases, it might also make sense to switch to
    using `bsdtar tf` instead of `bsdtar xf` when checking membership, but
    that's work for another day.
    23c2b82c