如何使用管道将 stderr 连接到 stdin?
“|”管道运算符将一个进程的标准输出连接到另一个进程的标准输入。有没有办法创建一个管道,将一个进程的 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用命名管道:
这应该保留 stdin &终端中“do-something”的 stdout 并将 stderr 重定向到 /my/pipe,由“error-handler”读取。
(我希望这个工作,没有 bash 来测试)
You could use named pipes:
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)
如果您愿意混合 stdouot 和 stderr,那么您可以首先将 stderr 重定向到 stdout,然后通过管道传输:
如果您不需要 stdout,则可以杀死它:
如果您确实想要也存储原始的标准输出,然后您必须将其重定向到一个文件(代替
/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:
If you don't want stdout, you can kill that one:
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 withexec
. Here are some details.(Unfortunately there is no direct "pipe this file descriptor" syntax like
2|
. That would have been handy.)您可以使用 bash 的进程替换功能获得此效果:
You can get this effect with bash's process substitution feature:
您还可以交换标准输出和标准输出。 stderr 流,即 stdout 成为新的 stderr,stderr 成为新的 stdout)。
You may also swap the stdout & stderr streams, i. e. stdout becomes the new stderr and stderr becomes the new stdout).