bash strerr pipe problem - workaround ?

James Gray james at gray.net.au
Sat Jun 14 08:27:11 UTC 2008


On 14/06/2008, at 6:16 PM, dave selby wrote:
> I have a app that outputs to stderr, what I need is a filtered version
> to be saved in a file
>
> motion -c %s/core/motion_conf/motion.conf 2> saved
>
> works as expected but is unfiltered. So to filter it I am trying ...
>
> motion -c %s/core/motion_conf/motion.conf | grep -E 'error|not  
> found' > saved

Hi Dave :)

Yep - because by the time you're redirecting STDERR to "saved", the  
output has already been sent to /dev/stderr (usually the screen).  The  
key is to remember that with shell redirection, ORDER IS IMPORTANT ;)

Try something like this:

motion -c %s/core/motion_conf/motion.conf 2>&1 | egrep 'error|not  
found' > saved

The magic is the "2>&1" which tells the shell, send STDERR to the same  
place as STDOUT.  As pipes (in the example above) read from STDIN, by  
redirecting STDERR->STDOUT we can then pipe the error stream as  
normal :)

Consider this:

some_command > output.file 2>&1

STDOUT is going to a file called "output.file", we then redirect  
STDERR to the same place as STDOUT which means we merge the streams  
into a single output file.  However, the following WILL NOT WORK AS IT  
FIRST APPEARS:

some_command 2>&1 > output.file

Think about it ;)  .... and read the "REDIRECTION" section in "man  
bash".

HTH,

James




More information about the ubuntu-users mailing list