如何使用管道将 stderr 连接到 stdin?

发布于 2024-12-04 18:06:28 字数 156 浏览 0 评论 0原文

“|”管道运算符将一个进程的标准输出连接到另一个进程的标准输入。有没有办法创建一个管道,将一个进程的 stderr 连接到另一个进程的标准输入,使标准输出在我的终端中保持活动状态?在互联网上搜索没有给我任何信息......

提前谢谢你, 米哈利斯.

The "|" pipe operator connects the stdout of one process to the stdin of another. Is there any way to create a pipe that connects the stderr of one process to the stdin of another keeping the stdout alive in my terminal? Searching on the internet gave me no information at all...

Thank you in advance,
Michalis.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

感情旳空白 2024-12-11 18:06:28

您可以使用命名管道:

mkfifo /my/pipe
error-handler </my/pipe &
do-something 2>/my/pipe

这应该保留 stdin &终端中“do-something”的 stdout 并将 stderr 重定向到 /my/pipe,由“error-handler”读取。

(我希望这个工作,没有 bash 来测试)

You could use named pipes:

mkfifo /my/pipe
error-handler </my/pipe &
do-something 2>/my/pipe

This should keep stdin & stdout of "do-something" in your terminal und redirect stderr to /my/pipe, which is read by "error-handler".

(I hope this work, have no bash to test)

标点 2024-12-11 18:06:28

如果您愿意混合 stdouot 和 stderr,那么您可以首先将 stderr 重定向到 stdout,然后通过管道传输:

theprogram 2>&1 | otherprogram

如果您不需要 stdout,则可以杀死它:

theprogram 2>&1 1> /dev/null | otherprogram

如果您确实想要也存储原始的标准输出,然后您必须将其重定向到一个文件(代替 /dev/null),或者重定向到您之前使用 exec. 这里有一些详细信息。

(不幸的是,没有直接“通过管道传输此文件”描述符”语法,如 2|。这会很方便。)

If you're happy to mix stdouot and stderr, then you can first redirect stderr to stdout and then pipe that:

theprogram 2>&1 | otherprogram

If you don't want stdout, you can kill that one:

theprogram 2>&1 1> /dev/null | otherprogram

If you do want to store the original stdout as well, then you have to redirect it either to a file (in place of /dev/null), or to another file descriptor that you opened previously with exec. Here are some details.

(Unfortunately there is no direct "pipe this file descriptor" syntax like 2|. That would have been handy.)

相权↑美人 2024-12-11 18:06:28

您可以使用 bash 的进程替换功能获得此效果:

somecommand 2> >(errorprocessor)

You can get this effect with bash's process substitution feature:

somecommand 2> >(errorprocessor)
桃扇骨 2024-12-11 18:06:28

您还可以交换标准输出和标准输出。 stderr 流,即 stdout 成为新的 stderr,stderr 成为新的 stdout)。

# only the stdout stream gets upcased
ls -ld / xxx ~/.bashrc yyy 3>&1 1>&2 2>&3 3>&- | tr '[[:lower:]]' '[[:upper:]]'

# block original stdout by closing fd 1
ls -ld / xxx ~/.bashrc yyy 2>&1 1>&- | tr '[[:lower:]]' '[[:upper:]]'

You may also swap the stdout & stderr streams, i. e. stdout becomes the new stderr and stderr becomes the new stdout).

# only the stdout stream gets upcased
ls -ld / xxx ~/.bashrc yyy 3>&1 1>&2 2>&3 3>&- | tr '[[:lower:]]' '[[:upper:]]'

# block original stdout by closing fd 1
ls -ld / xxx ~/.bashrc yyy 2>&1 1>&- | tr '[[:lower:]]' '[[:upper:]]'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文