如何将通过 unix 管道传输的数据写入当前终端 window?

How can I write the data coming through a unix pipe to the current terminal window?

我正在寻找与 pv 类似的功能,但我不想输出统计信息,而是希望将整个管道吞吐量打印到终端。这可能吗,例如使用 pvtee 或一些巧妙的流重定向?

在同一终端查看管道内容window

在 Linux 上,您可以使用

 someCommand | tee /dev/tty | anotherCommand

例如

 { for i in 1 2 3 4; do echo $i; sleep 0.5; done; } | tee /dev/tty | grep 3

打印

1
2
3
3
4

3出现了两次,因为grep也打印了它。因为您无法分辨哪个输出来自 tee 哪个输出来自 grep 我建议仅当管道不自行打印任何内容时才使用此管道内容查看器,例如如果管道中的最后一个命令重定向到一个文件。

在另一个终端查看管道内容window

或者,您可以使用

在另一个终端中查看管道的内容
# first, execute this in the terminal that should view the pipe's content
mkfifo /tmp/mypipeviewer
cat /tmp/mypipeviewer

# then run your pipe in another terminal
someCommand | tee /tmp/mypipeviewer | anotherCommand

这仅在 cat 为 运行 时有效。如果您退出 cat(例如通过按 ctrlC 或关闭其终端)管道将提前退出。