如何在将标准输出保持在屏幕上的同时通过管道传输? (而不是输出文件)

发布于 2024-11-01 22:12:13 字数 406 浏览 7 评论 0原文

我想通过管道传输程序的标准输出,同时将其保留在屏幕上。

举一个简单的例子(这里使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时常饿 2024-11-08 22:12:13

这是一个适用于任何 Unix / Linux 实现的解决方案,假设它遵循 POSIX 标准。它也适用于一些非 Unix 环境,例如 cygwin。

echo 'ee' | tee /dev/tty | foo

参考:The Open Group 基本规范第 7 期
IEEE 标准 1003.1,2013 年版,§10.1

/dev/tty

与该进程的进程组关联(如果有)。 它是
对于希望确定的程序或 shell 过程很有用
无论如何,向终端写入消息或从终端读取数据
输出已被重定向。
它也可用于以下应用程序:
当需要键入输出时,要求输出文件的名称,并且
找出当前正在使用的终端是很烦人的。在每个进程中,控制终端的同义词

报道,像 Google Colab 这样的环境在仍然具有 tty 命令返回可用设备。这是一个解决方法:

tty=$(tty)
echo 'ee' | tee $tty | foo

或者使用古老的 Bourne shell:

tty=`tty`
echo 'ee' | tee $tty | foo

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.

echo 'ee' | tee /dev/tty | foo

Reference: 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 to
or 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

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:

tty=$(tty)
echo 'ee' | tee $tty | foo

or with an ancient Bourne shell:

tty=`tty`
echo 'ee' | tee $tty | foo
筱果果 2024-11-08 22:12:13

另一件要尝试的事情是:

echo 'ee' | tee >(foo)

>(foo)进程替换

编辑
为了更清楚一点,(.) 这里启动一个新的子进程到当前终端,输出将被重定向到该终端。

echo ee | tee >(wc | grep 1)
#              ^^^^^^^^^^^^^^ => child process

除了子进程中的任何变量声明/更改不会反映在父进程中之外,很少有人担心在子进程中运行命令。

Another thing to try is:

echo 'ee' | tee >(foo)

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.

echo ee | tee >(wc | grep 1)
#              ^^^^^^^^^^^^^^ => child process

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.

月依秋水 2024-11-08 22:12:13

尝试:

$ echo 'ee' | tee /dev/stderr | foo

当然,如果使用 stderr 是一种选择。

Try:

$ echo 'ee' | tee /dev/stderr | foo

If using stderr is an option, of course.

海之角 2024-11-08 22:12:13

在某些系统上,对“/dev/stdout”的访问被拒绝,但对用户终端的访问由“/dev/tty”提供。
使用“wc”代表“foo”,上面的示例可以正常工作(在 Linux、OSX 等上),如下所示:

% echo '嗨' |三通 /dev/tty |厕所
你好
1 1 3

要在匹配文件列表的底部添加计数,我使用类似以下内容的内容:
<代码>% ls [AJ]* |三通 /dev/tty | wc -l

为了避免记住所有这些,我定义了别名:
% 别名 t tee /dev/tty
% alias wcl wc -l

这样我就可以简单地说:
<代码>% ls [AJ]* | t | wcl


后记:对于年轻一代来说,他们可能会嘲笑它的发音“titty”,我可能会补充说“tty”曾经是常见的
“电传打字机”终端的缩写,使用一卷黄色
纸张和圆形钥匙经常被卡住。

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:

% 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

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


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.

魄砕の薆 2024-11-08 22:12:13

首先,您需要找出与您的屏幕(或您希望输出显示在哪个屏幕上)关联的终端:

tty

然后您可以将输出发送到该终端,并通过 foo 程序传输另一个副本:

echo ee | tee /dev/pty/2 | foo

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:

echo ee | tee /dev/pty/2 | foo
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文