Linux Shell:将 stdout 重定向到 /dev/null 并将 stderr 重定向到 stdout
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12027170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shell: redirect stdout to /dev/null and stderr to stdout
提问by Shou Ya
I saw this interesting question at a comment on cyberciti.biz.
我在cyberciti.biz的评论中看到了这个有趣的问题。
That I found I even can't find a flexible way to do this in one-line command with sh.
我发现我什至找不到一种灵活的方法来使用 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. :)
欢迎分享您对此的看法。:)
采纳答案by geirha
You want
你要
./script 2>&1 1>/dev/null | ./other-script
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
这里的顺序很重要。让我们假设 stdin (fd 0)、stdout (fd 1) 和 stderr (fd 2) 最初都连接到 tty,所以
0: /dev/tty, 1: /dev/tty, 2: /dev/tty
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:
设置的第一件事是管道。other-script 的 stdin 连接到管道,脚本的 stdout 连接到管道,所以到目前为止脚本的文件描述符如下所示:
0: /dev/tty, 1: pipe, 2: /dev/tty
Next, the redirections occur, from left to right. 2>&1
makes fd 2 go wherever fd 1 is currently going, which is the pipe.
接下来,重定向发生,从左到右。2>&1
使 fd 2 去到 fd 1 当前去的地方,也就是管道。
0: /dev/tty, 1: pipe, 2: pipe
Lastly, 1>/dev/null
redirects fd1 to /dev/null
最后,1>/dev/null
将 fd1 重定向到/dev/null
0: /dev/tty, 1: /dev/null, 2: pipe
End result, script's stdout is silenced, and its stderr is sent through the pipe, which ends up in other-script's stdin.
最终结果,脚本的标准输出被静音,它的标准错误通过管道发送,最终进入其他脚本的标准输入。
Also see http://bash-hackers.org/wiki/doku.php/howto/redirection_tutorial
另请参阅http://bash-hackers.org/wiki/doku.php/howto/redirection_tutorial
Also note that 1>/dev/null
is synonymous to, but more explicit than >/dev/null
另请注意,这1>/dev/null
是同义词,但比>/dev/null
回答by Charlie Martin
Well, that's because you can't. STDOUT and STDERR are just two files, represented by file descriptors, which are just integers, specifically 1 and 2.
嗯,那是因为你不能。STDOUT 和 STDERR 只是两个文件,用文件描述符表示,它们只是整数,特别是 1 和 2。
What you're asking is to set descriptor 2 to /dev/null
, then set descriptor 3 to the same file descriptor 2 and have that output go somewhere else.
您要问的是将描述符 2 设置为/dev/null
,然后将描述符 3 设置为相同的文件描述符 2 并将该输出放在其他地方。
回答by Zaar Hai
How about this:
这个怎么样:
./script 3>&1 1>/dev/null 2>&3 | ./other-script
The idea is to "backup" stdout descriptor, close the original stdout and then redirect strerr to saved stdout.
这个想法是“备份”标准输出描述符,关闭原始标准输出,然后将 strerr 重定向到保存的标准输出。
Its much similar to the solution provided by geirha, but its more explicit (bash coding can easily become very obscured).
它与 geirha 提供的解决方案非常相似,但更明确(bash 编码很容易变得非常模糊)。