壳牌:stdout重定向到/ dev / null的和标准错误到标准输出

编程入门 行业动态 更新时间:2024-10-23 03:22:09
本文介绍了壳牌:stdout重定向到/ dev / null的和标准错误到标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我看到了这个有趣的问题在上cyberciti.biz.

I saw this interesting question at a comment on cyberciti.biz.

这是我发现我甚至无法找到一个灵活的方式与SH单行命令来做到这一点。

That I found I even can't find a flexible way to do this in one-line command with sh.

据我对解决方案的想法是:

As far my thought for the solution is:

tmp_file=`mktemp` (./script 2>$tmp_file >/dev/null; cat $tmp_file) | ./other-script rm tmp_file

但是你看,这是不是同步的,致命的,它是如此的丑陋。

But you see, this is not synchronous, and fatally, it's so ugly.

欢迎分享你介意这个。 :)

Welcome to share you mind about this. :)

推荐答案

您想

./script 2>&1 >/dev/null | ./other-script

这里的顺序很重要。假设标准输入(FD 0),由于输出(FD 1)和stderr(FD 2)都连接到一个tty最初,所以

The order here is important. Let's assume stdin (fd 0), stdout (fd 1) and stderr (fd 2) are all connected to a tty initially, so

0: /dev/tty, 1: /dev/tty, 2: /dev/tty

这是被设立的第一件事就是管道。其他脚本的标准输入被连接到管道和脚本的stdout被连接到管道,所以脚本的文件描述符到目前为止是这样的:

The first thing that gets set up is the pipe. other-script's stdin gets connected to the pipe, and script's stdout gets connected to the pipe, so script's file descriptors so far look like:

0: /dev/tty, 1: pipe, 2: /dev/tty

接下来,发生了重定向,从左到右。 2 - ;&放大器; 1 使得FD 2去的地方FD 1正在准备,这是管

Next, the redirections occur, from left to right. 2>&1 makes fd 2 go wherever fd 1 is currently going, which is the pipe.

0: /dev/tty, 1: pipe, 2: pipe

最后,>的/ dev / null的重定向到FD1 的/ dev / null的

0: /dev/tty, 1: /dev/null, 2: pipe

最终的结果,脚本的stdout是沉默的,其标准错误是通过管道,这在其他脚本的标准输入结束发送。

End result, script's stdout is silenced, and its stderr is sent through the pipe, which ends up in other-script's stdin.

另请参阅bash-hackers/wiki/doku.php/howto/redirection_tutorial

更多推荐

壳牌:stdout重定向到/ dev / null的和标准错误到标准输出

本文发布于:2023-07-06 07:34:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047345.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标准   壳牌   重定向   错误   dev

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!