如何在将标准输出保留在屏幕上的同时对其进行管道传输?(而不是输出文件) [英] How to pipe stdout while keeping it on screen ? (and not to a output file)

查看:20
本文介绍了如何在将标准输出保留在屏幕上的同时对其进行管道传输?(而不是输出文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to pipe standard output of a program while keeping it on screen.

举一个简单的例子(echo 此处仅用于说明目的):

With a simple example (echo use here is just for illustration purpose) :

$ echo 'ee' |foo
ee <- 我想看到的输出

$ echo 'ee' | foo
ee <- the output I would like to see

我知道 tee 可以将标准输出复制到文件中,但这不是我想要的.
$ echo 'ee' |三通输出.txt |foo

I know tee could copy stdout to file but that's not what I want.
$ echo 'ee' | tee output.txt | foo

我试过了
$ echo 'ee' |三通/开发/标准输出|foo 但它不起作用,因为到 /dev/stdout 的 tee 输出通过管道传输到 foo

I tried
$ echo 'ee' | tee /dev/stdout | foo but it does not work since tee output to /dev/stdout is piped to foo

推荐答案

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

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

参考:The Open Group Base Specifications Issue 7IEEE 标准 1003.1,2013 年版,§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

据报道,某些环境(例如 Google Colab)未实施 /dev/tty,但仍具有 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:

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

或者使用古老的 Bourne shell:

or with an ancient Bourne shell:

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

这篇关于如何在将标准输出保留在屏幕上的同时对其进行管道传输?(而不是输出文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆