Linux Pipe, Warning Messages, stdout

linuxpipestdout

This is probably the best place to ask the question in its simplest form. I am writing a script which takes output of a command and puts it into a variable. After some debugging I figured out the command when it errors does not goto stdout but to the console (or wall). See example below:

When the command runs successfully

root@local# apcaccess status
APC : 700
VERSION : xxxxx
BATTSTAT : AC

When the command errors out

root@local# apcaccess status
apcupsd @ localhost: Connection Refused.

root@local# apcaccess status | grep -i version
apcupsd @ localhost: Connection Refused.

I have noticed when I try to pipe the command through grep 'apcaccess status | grep -i version' I will get the version number on a successful run but on the system that it errors out on I get the error message. I believe it is not going through stdout.

The question is: how can I force the output to goto stdout?

Best Answer

# apcaccess status 2>&1 | grep -i version

This will redirect stderr to stdout, so grep will see the output.