Operating System - HP-UX
1820254 Members
2610 Online
109622 Solutions
New Discussion юеВ

How to redirect STDERR to STDOUT for shell command execution ?

 
ashokd
Occasional Contributor

How to redirect STDERR to STDOUT for shell command execution ?

Hi,

I am going to use popen, faceing one difficulty to get the console output when command execution fails.

In Suse10 i am doing this way,

popen("ls -l aa > /dev/stdout 2>&1") where 'aa' directory is not present.

Actually here i am gettting error string from console without creating any extra temporary file.

How can i do the thing in HP-UX ?

-ashokd
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor

Re: How to redirect STDERR to STDOUT for shell command execution ?

Hi :

The order of redirection matters. I assume that you want to discard STDERR messages like those for non-existent operands.

# ls -l aa 2>/dev/null

In your case, you assigned file descriptor 2 to be the same as descriptor 1 whereas you want them segregated.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: How to redirect STDERR to STDOUT for shell command execution ?

Hi (again):

What hasn't been answered?

If you want to capture STDOUT in a temporary file, do something like:

# ls -l aa 2>/dev/null > mylog

...or, to write STDOUT to both your terminal and a file:

# ls -l aa 2>/dev/null | tee mylog

in either case, if 'aa' doesn't exist, 'mylog' will be empty by will be created anyway. Change '>' to '>>' or add '-a' to the 'tee' command to append to the file.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: How to redirect STDERR to STDOUT for shell command execution ?

>facing one difficulty to get the console output when command execution fails.
popen("ls -l aa > /dev/stdout 2>&1")

You don't want that output to go to the program? You could try /dev/tty instead.
Laurent Menase
Honored Contributor

Re: How to redirect STDERR to STDOUT for shell command execution ?

popen("ls -l aa 2>&1")
if you want to get the result of the ls and the error messages on the pipe

popen("ls -l aa >/dev/tty 2>&1")
if you want to get the result on the current control terminal for that session.

popen("ls -l aa 2>&1 >/dev/tty") if you want to get the result to the control tty of the session but the error message to the pipe.