壳牌:stdout重定向到/ dev / null的和标准错误到标准输出 [英] Shell: redirect stdout to /dev/null and stderr to stdout

查看:329
本文介绍了壳牌:stdout重定向到/ dev / null的和标准错误到标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了这个有趣的问题在上<一个评论href=\"http://www.cyberciti.biz/faq/how-to-redirect-output-and-errors-to-devnull/\">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

最后,&GT;的/ 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.

另请参阅<一个href=\"http://bash-hackers.org/wiki/doku.php/howto/redirection_tutorial\">http://bash-hackers.org/wiki/doku.php/howto/redirection_tutorial

这篇关于壳牌:stdout重定向到/ dev / null的和标准错误到标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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