bash 如何在将标准输出保留在屏幕上的同时进行管道输出?(而不是输出文件)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5677201/
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 to pipe stdout while keeping it on screen ? (and not to a output file)
提问by gentooboontoo
I would like to pipe standard output of a program while keeping it on screen.
我想通过管道传输程序的标准输出,同时将其保留在屏幕上。
With a simple example (echo
use here is just for illustration purpose) :
举一个简单的例子(echo
此处仅用于说明目的):
$ echo 'ee' | foo
ee
<- the output I would like to see
$ echo 'ee' | foo
ee
<- 我想看到的输出
I know tee could copy stdout to file but that's not what I want.$ echo 'ee' | tee output.txt | foo
我知道 tee 可以将 stdout 复制到文件中,但这不是我想要的。$ echo 'ee' | tee output.txt | foo
I tried$ echo 'ee' | tee /dev/stdout | foo
but it does not work since tee output to /dev/stdout
is piped to foo
我试过了,$ echo 'ee' | tee /dev/stdout | foo
但它不起作用,因为 tee 输出到/dev/stdout
管道foo
回答by jlliagre
Here is a solution that works at on any Unix / Linux implementation, assuming it cares to follow the POSIX
standard. It works on some non Unix environments like cygwin
too.
这是一个适用于任何 Unix / Linux 实现的解决方案,假设它关心遵循POSIX
标准。它也适用于一些非 Unix 环境cygwin
。
echo 'ee' | tee /dev/tty | foo
Reference: The Open Group Base Specifications Issue 7 IEEE Std 1003.1, 2013 Edition, §10.1:
参考: The Open Group Base Specifications Issue 7 IEEE Std 1003.1, 2013 Edition, §10.1:
/dev/tty
Associated with the process group of that process, if any. It is useful for programs or shell procedures that wish to be sure of writing messages toor reading data from the terminal no matter how output has been redirected.It can also be used for applications that demand the name of a file for output, when typed output is desired and it is tiresome to find out what terminal is currently in use. In each process, a synonym for the controlling terminal
/开发/ tty
与该进程的进程组相关联(如果有)。对于希望确保无论输出如何重定向都可以向终端写入消息或从终端读取数据的程序或外壳程序非常有用。它还可以用于需要输出文件名的应用程序,当需要键入输出并且查找当前正在使用的终端很烦人时。在每个进程中,控制终端的同义词
Some environments like Google Colab have been reported not to implement /dev/tty
while still having their tty
command returning a usable device. Here is a workaround:
据报道,某些环境(例如 Google Colab)/dev/tty
在其tty
命令返回可用设备的同时未实施。这是一个解决方法:
tty=$(tty)
echo 'ee' | tee $tty | foo
or with an ancient Bourne shell:
或使用古老的 Bourne shell:
tty=`tty`
echo 'ee' | tee $tty | foo
回答by bmk
Another thing to try is:
另一件事要尝试是:
echo 'ee' | tee >(foo)
The >(foo)
is a process substitution.
这>(foo)
是一个过程替换。
回答by user51527
Access to "/dev/stdout" is denied on some systems, but access to the user terminal is given by "/dev/tty". Using "wc" for "foo", the above examples work OK (on linux, OSX, etc.) as:
在某些系统上,对“/dev/stdout”的访问被拒绝,但对用户终端的访问由“/dev/tty”提供。对“foo”使用“wc”,上面的例子可以正常工作(在 linux、OSX 等上):
% echo 'Hi' | tee /dev/tty | wc Hi 1 1 3
% echo 'Hi' | tee /dev/tty | wc Hi 1 1 3
To add a count at the bottom of a list of matching files, I use something like:% ls [A-J]* | tee /dev/tty | wc -l
要在匹配文件列表的底部添加一个计数,我使用如下内容:% ls [A-J]* | tee /dev/tty | wc -l
To avoid having to remember all this, I define aliases:% alias t tee /dev/tty
% alias wcl wc -l
为了避免记住所有这些,我定义了别名:% alias t tee /dev/tty
% alias wcl wc -l
so that I can simply say:% ls [A-J]* | t | wcl
所以我可以简单地说:% ls [A-J]* | t | wcl
POSTSCRIPT: For the younger set, who might titter at its pronunciation as "titty", I might add that "tty" was once the common abbreviation for a "teletype" terminal, which used a roll of yellow paper and had round keys that often stuck.
POSTSCRIPT:对于那些可能会嘲笑“titty”的发音的年轻人,我想补充一点,“tty”曾经是“电传打字机”终端的常见缩写,它使用一卷黄纸,并有经常使用的圆键卡住。
回答by Jan
Try:
尝试:
$ echo 'ee' | tee /dev/stderr | foo
If using stderr is an option, of course.
当然,如果使用 stderr 是一种选择。
回答by Michael Martinez
first you need to figure out the terminal associated with your screen (or whichever screen you want the output to display on):
首先,您需要找出与您的屏幕(或您希望输出显示在哪个屏幕上)相关联的终端:
tty
then you can tee the output to that terminal and pipe the other copy through your foo program:
然后您可以将输出发送到该终端并通过您的 foo 程序通过管道传输另一个副本:
echo ee | tee /dev/pty/2 | foo