如何将标准输入压缩到文件中并将标准输入打印到标准输出?
我想执行一个命令,将该命令的输出即时进行 gzip 压缩,并且还回显/tee 输出该命令的输出。
即,类似:
echo "hey hey, we're the monkees" | gzip --stdout > my_log.gz
除了该行执行时,我想在标准输出上看到这一点:
hey hey, we're the monkees
I want to execute a command, have the output of that command get gzip'd on the fly, and also echo/tee out the output of that command.
i.e., something like:
echo "hey hey, we're the monkees" | gzip --stdout > my_log.gz
Except when the line executes, I want to see this on standard out:
hey hey, we're the monkees
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另一种方式(假设像
bash
或zsh
这样的 shell):公认奇怪的
>()
语法基本上执行以下操作:/tmp/
中的内容)()
内执行命令并将 FIFO 绑定到该子命令上的标准输入那么,tee 最终看到的是这样的:
所有
gzip
看到的是它的标准输入。对于 Bash,请参阅
man bash
了解详细信息。 它位于重定向部分。 对于 Zsh,请参阅“进程”标题下的man zshexpn
代换。”据我所知,Korn Shell、经典 Bourne Shell 的变体(包括 ash 和 dash)以及 C Shell 不支持此语法。
Another way (assuming a shell like
bash
orzsh
):The admittedly strange
>()
syntax basically does the following:/tmp/
)()
and bind the FIFO to stdin on that subcommandWhat
tee
ends up seeing, then, is something like:All
gzip
sees is its standard input.For Bash, see
man bash
for details. It's in the section on redirection. For Zsh, seeman zshexpn
under the heading "Process Substitution."As far as I can tell, the Korn Shell, variants of the classic Bourne Shell (including ash and dash), and the C Shell don't support this syntax.
正如评论中指出的,在某些情况下,
/dev/stdout
可能比/dev/tty
工作得更好。As pointed out in the comments,
/dev/stdout
might work better than/dev/tty
in some circumstances.喝一杯漂亮的 T 恤!
因为我度过了一个缓慢的下午,这里有一些精彩的说明性 ascii-art...
正如 greyfade 在另一个答案中演示的那样,“文件”不必是常规文件,但可以是 FIFO,让您可以通过管道传输该文件输出到第三个命令。
Have a nice cup of tee!
As I'm having a slow afternoon, here's some gloriously illustrative ascii-art...
As greyfade demonstrates in another answer the 'file' need not be a regular file, but could be FIFO letting you pipe that tee'd output into a third command.
只是发布一种不涉及触摸磁盘的方法:
Just to post a way that doesn't involve touching disk: