linux64-bitbzip2linux-from-scratch

Why Using "< /dev/null" with a program like "bzip2"?


bzip2 --version 2>&1 < /dev/null | head -n1 | cut -d" " -f1,7-

I saw this code in the LFS book, what is the purpose of the < /dev/null there? I know < /dev/null is used to prevent programs from waiting input by sending zeroes, but is it necessary here?


Solution

  • Yes, this is necessary.

    As of the current version 1.0.8, bzip2 --version will print version info but it will also continue compressing stdin:

    $ ./bzip2 --version
    bzip2, a block-sorting file compressor.  Version 1.0.8, 13-Jul-2019.
    
       Copyright (C) 1996-2019 by Julian Seward.
    
       This program is free software; [...]
    
    bzip2: I won't write compressed data to a terminal.
    bzip2: For help, type: `bzip2 --help'.
    

    When additionally piping through head, it'll simply hang, waiting for data on stdin. < /dev/null prevents this by presenting a zero-length file it can compress instead. (This does add some binary garbage to the end of the output, but it's filtered out by the head so it doesn't matter).

    Debian (and its downstreams like Ubuntu) will patch this out, making the < /dev/null unnecessary:

    @@ -1916,8 +1918,8 @@ IntNative main ( IntNative argc, Char *a
           if (ISFLAG("--keep"))              keepInputFiles   = True;    else
           if (ISFLAG("--small"))             smallMode        = True;    else
           if (ISFLAG("--quiet"))             noisy            = False;   else
    -      if (ISFLAG("--version"))           license();                  else
    -      if (ISFLAG("--license"))           license();                  else
    +      if (ISFLAG("--version"))           { license(); exit ( 0 ); }  else
    +      if (ISFLAG("--license"))           { license(); exit ( 0 ); }  else
           if (ISFLAG("--exponential"))       workFactor = 1;             else
           if (ISFLAG("--repetitive-best"))   redundant(aa->name);        else
           if (ISFLAG("--repetitive-fast"))   redundant(aa->name);        else
    

    But obviously, Linux From Scratch does not benefit from any distro specific patches.