如何在将标准输出保持在屏幕上的同时通过管道传输? (而不是输出文件)
我想通过管道传输程序的标准输出,同时将其保留在屏幕上。
举一个简单的例子(这里使用 echo
仅用于说明目的):
$ echo 'ee' | foo
ee
<- 我想看到的输出
我知道 tee 可以将 stdout 复制到文件,但这不是我想要的。
$ echo 'ee' | $ echo 'ee' |三通输出.txt | foo
我试过
$ echo 'ee' | $ echo 'ee' |三通 /dev/stdout | foo
但它不起作用,因为到 /dev/stdout
的 tee 输出通过管道传输到 foo
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 'ee' | foo
ee
<- the output I would like to see
I know tee could copy stdout to file but that's not what I want.$ 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个适用于任何 Unix / Linux 实现的解决方案,假设它遵循 POSIX 标准。它也适用于一些非 Unix 环境,例如 cygwin。
参考:The Open Group 基本规范第 7 期
IEEE 标准 1003.1,2013 年版,§10.1:
报道,像 Google Colab 这样的环境在仍然具有
tty
命令返回可用设备。这是一个解决方法:或者使用古老的 Bourne shell:
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 likecygwin
too.Reference: The Open Group Base Specifications Issue 7
IEEE Std 1003.1, 2013 Edition, §10.1:
Some environments like Google Colab have been reported not to implement
/dev/tty
while still having theirtty
command returning a usable device. Here is a workaround:or with an ancient Bourne shell:
另一件要尝试的事情是:
>(foo)
是 进程替换。编辑:
为了更清楚一点,(.) 这里启动一个新的子进程到当前终端,输出将被重定向到该终端。
除了子进程中的任何变量声明/更改不会反映在父进程中之外,很少有人担心在子进程中运行命令。
Another thing to try is:
The
>(foo)
is a process substitution.Edit:
To make a bit clearer, (.) here start a new child process to the current terminal, where the output is being redirected to.
Except that any variable declarations/changes in child process do not reflect in the parent, there is very few of concern with regard to running commands in a child process.
尝试:
当然,如果使用 stderr 是一种选择。
Try:
If using stderr is an option, of course.
在某些系统上,对“/dev/stdout”的访问被拒绝,但对用户终端的访问由“/dev/tty”提供。
使用“wc”代表“foo”,上面的示例可以正常工作(在 Linux、OSX 等上),如下所示:
要在匹配文件列表的底部添加计数,我使用类似以下内容的内容:
<代码>% ls [AJ]* |三通 /dev/tty | wc -l
为了避免记住所有这些,我定义了别名:
% 别名 t tee /dev/tty
% alias wcl wc -l
这样我就可以简单地说:
<代码>% ls [AJ]* | t | wcl
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:
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
To avoid having to remember all this, I define aliases:
% alias t tee /dev/tty
% alias wcl wc -l
so that I can simply say:
% ls [A-J]* | t | wcl
首先,您需要找出与您的屏幕(或您希望输出显示在哪个屏幕上)关联的终端:
然后您可以将输出发送到该终端,并通过 foo 程序传输另一个副本:
first you need to figure out the terminal associated with your screen (or whichever screen you want the output to display on):
then you can tee the output to that terminal and pipe the other copy through your foo program: