Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell: redirect stdout to /dev/null and stderr to stdout [duplicate]

Tags:

linux

bash

shell

sh

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

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. :)

like image 610
Shou Ya Avatar asked Aug 19 '12 14:08

Shou Ya


People also ask

How do I redirect stderr to stdout in shell?

Understanding the concept of redirections and file descriptors is very important when working on the command line. To redirect stderr and stdout , use the 2>&1 or &> constructs.

How do I redirect only stderr to dev Null?

How do I redirect the output of stderr to stdout, and then redirect this combined output to /dev/null device? In Unix, how do I redirect error messages to /dev/null? You can send output to /dev/null, by using command >/dev/null syntax.

How do I redirect stdout and stderr to the same location?

Solution. Use the shell syntax to redirect standard error messages to the same place as standard output. where both is just our (imaginary) program that is going to generate output to both STDERR and STDOUT.


2 Answers

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

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:

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.

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

Lastly, 1>/dev/null redirects fd1 to /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

Also note that 1>/dev/null is synonymous to, but more explicit than >/dev/null

like image 82
geirha Avatar answered Sep 23 '22 14:09

geirha


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.

Its much similar to the solution provided by geirha, but its more explicit (bash coding can easily become very obscured).

like image 25
Zaar Hai Avatar answered Sep 21 '22 14:09

Zaar Hai