为什么将“ dev / null”用于“ bzip2”之类的程序?

问题描述 投票:0回答:2

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

我在LFS书中看到了这段代码,那里的< /dev/null的目的是什么?我知道< /dev/null用于防止程序通过发送零来等待输入,但是这里是否有必要?

linux 64-bit bzip2 linux-from-scratch
2个回答
0
投票

是,这是必需的。

从当前版本1.0.8开始,bzip2 --version将打印版本信息,但也会继续压缩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'.

[另外通过head进行管道传输时,它只会挂起,等待stdin上的数据。 < /dev/null通过提供一个可以压缩的零长度文件来防止这种情况。 (这确实在输出的末尾添加了一些二进制垃圾,但是head过滤掉了它,所以没关系)。

Debian(及其下游版本,如Ubuntu)将patch this out,使< /dev/null变得不必要:

@@ -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

但是很明显,Linux From Scratch没有从任何发行版特定的补丁中受益。


0
投票
对于bzip2,

短语< /dev/nullstdin

© www.soinside.com 2019 - 2024. All rights reserved.