Mse*_*ade 6 io-redirection bzip2
所以如果我输入命令
$ gzip --version | head -n1
Run Code Online (Sandbox Code Playgroud)
一切都按预期进行。但是如果我用 bzip2 尝试同样的方法:
$ bzip2 --version | head -n1
Run Code Online (Sandbox Code Playgroud)
我有很多行,我必须按Ctrl-C终止。
这里发生了什么?
编辑:
被打印的行
$ bzip2 --version | head -n1
bzip2, a block-sorting file compressor. Version 1.0.6, 6-Sept-2010.
Copyright (C) 1996-2010 by Julian Seward.
This program is free software; you can redistribute it and/or modify
it under the terms set out in the LICENSE file, which is included
in the bzip2-1.0.6 source distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
LICENSE file for more details.
Run Code Online (Sandbox Code Playgroud)
我必须按Ctrl-C继续。
如果我省略管道我得到
$ bzip2 --version
bzip2, a block-sorting file compressor. Version 1.0.6, 6-Sept-2010.
Copyright (C) 1996-2010 by Julian Seward.
This program is free software; you can redistribute it and/or modify
it under the terms set out in the LICENSE file, which is included
in the bzip2-1.0.6 source distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
LICENSE file for more details.
bzip2: I won't write compressed data to a terminal.
bzip2: For help, type: `bzip2 --help'.
Run Code Online (Sandbox Code Playgroud)
如果我将 stdout 与 stderr 合并为 @devnull sais,它会很好地显示行,但我需要按Ctrl- C。我试过
$ gcc 2>&1 | head -n1
Run Code Online (Sandbox Code Playgroud)
它运行良好,所以我认为bzip2命令中仍然缺少一些东西。
编辑2:
我用以下命令解决了这个问题:
$ bzip2 --version 2>&1 < /dev/null | head -n1
Run Code Online (Sandbox Code Playgroud)
但我还是不明白问题所在。
不同之处在于gzip --version输出到STDOUT而bzip2 --version输出到STDERR。
合并STDERR到STDOUT你会看到预期的:
$ bzip2 --version 2>&1 | head -n1
bzip2, a block-sorting file compressor. Version 1.0.6, 6-Sept-2010.
Run Code Online (Sandbox Code Playgroud)