bash 如何将标准 gzip 压缩到文件中并将标准打印到标准输出中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/570984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I gzip standard in to a file and also print standard in to standard out?
提问by Ross Rogers
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.
我想执行一个命令,让该命令的输出即时得到 gzip,并且还回显/输出该命令的输出。
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
回答by greyfade
Another way (assuming a shell like bashor zsh):
另一种方式(假设外壳像bashor zsh):
echo "hey hey, we're the monkees" | tee >(gzip --stdout > my_log.gz)
The admittedly strange >()syntax basically does the following:
公认的奇怪>()语法基本上执行以下操作:
- Create new FIFO (usually something in
/tmp/) - Execute command inside
()and bind the FIFO to stdin on that subcommand - Return FIFO filename to command line.
- 创建新的 FIFO(通常是 中的东西
/tmp/) - 在内部执行命令
()并将 FIFO 绑定到该子命令的 stdin - 将 FIFO 文件名返回到命令行。
What teeends up seeing, then, is something like:
那么,tee最终看到的是:
tee /tmp/arjhaiX4
All gzipsees is its standard input.
所有gzip看到的是它的标准输入。
For Bash, see man bashfor details. It's in the section on redirection. For Zsh, see man zshexpnunder the heading "Process Substitution."
对于 Bash,请参阅man bash有关详细信息。它在关于重定向的部分。对于 Zsh,请参阅man zshexpn标题“进程替换”下的内容。
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.
据我所知,Korn Shell、经典 Bourne Shell 的变体(包括 ash 和 dash)和 C Shell 不支持这种语法。
回答by Paul Tomblin
echo "hey hey, we're the monkees" | tee /dev/tty | gzip --stdout > my_log.gz
As pointed out in the comments, /dev/stdoutmight work better than /dev/ttyin some circumstances.
正如评论中指出的那样,/dev/stdout可能比/dev/tty某些情况下工作得更好。
回答by Paul Dixon
Have a nice cup of tee!
喝一杯好茶!
The tee command copies standard input to standard output and also to any files given as arguments. This is useful when you want not only to send some data down a pipe, but also to save a copy
tee 命令将标准输入复制到标准输出以及作为参数给出的任何文件。当您不仅要通过管道发送一些数据,而且要保存副本时,这很有用
As I'm having a slow afternoon, here's some gloriously illustrative ascii-art...
由于我有一个缓慢的下午,这里有一些精彩的说明性 ascii 艺术......
+-----+ +---+ +-----+
stdin -> |cmd 1| -> stdout -> |tee| -> stdout -> |cmd 2|
+-----+ +---+ +-----+
|
v
file
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.
正如 greyfade 在另一个答案中所展示的,“文件”不必是常规文件,而可以是 FIFO,让您将 tee 的输出通过管道传输到第三个命令中。
+-----+ +---+ +-----+
stdin -> |cmd 1| -> stdout -> |tee| -> stdout -> |cmd 2|
+-----+ +---+ +-----+
|
v
FIFO
|
v
+-----+
|cmd 3|
+-----+
回答by Joshua
Just to post a way that doesn't involve touching disk:
只是发布一种不涉及触摸磁盘的方式:
echo "hey hey, we're the monkees" | (exec 1>&3 && tee /proc/self/fd/3 | gzip --stdout > my_log.gz)

