views:

28

answers:

1

Hi,

I need to capture the output and error of a command in my bash script and know whether the command succeeded or not.

At the moment, I am capturing both like this:

output=$(mycommand 2>&1)

I then need to check the exit value of mycommand. If it failed, I need to do some stuff with the output, if the command succeeded, I don't need to touch the output.

Since I am capturing the output, checking $? is always a 0 since bash succeeded at capturing the output into the variable.

This is a very time sensitive script, so we are trying to avoid any slower solutions like outputting to a file and re-reading it in.

If I could capture stdout to one variable and stderr to another, that would solve my problem because I could just check if the error variable was empty or not.

Thanks.

+2  A: 

What version of bash are you using? The capture of the output has zero effect on the return code with my version, 4.1.5:

pax> false; echo $?
1
pax> echo $?
0
pax> x=$(false 2>&1) ; echo $?
1

It's not always a good idea to rely on standard error being non-empty to detect errors. Many programs don't output errors but rely solely on the return code.

paxdiablo
I forgot to mention that I pipe the output of my command to another. Although I can do that after wards in a separate command. Thanks.
mhost