PLSmA1
PLSmA1

Reputation: 23

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?

Upvotes: 0

Views: 290

Answers (2)

Ryan
Ryan

Reputation: 443

The phrase < /dev/null is stdin for bzip2. Quite possibly, it used to be required or just good practice to account for each standard stream and the author is old enough to still be doing it. The three standard streams are stdin, stdout and stderr and all are being used here.

I personally would do bzip2 --version 2>&1 | head -n1 | cut -d" " -f1,7- since bzip2 --version will not fail by missing stdin.

It may just be required for LFS. It is not required for Ubuntu.

Upvotes: 1

that other guy
that other guy

Reputation: 123650

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.

Upvotes: 2

Related Questions