capturing stderr from windows

B

Brian

I wrote a perl script which invokes system calls using system().

I want to be able to redirect the output of these calls to a file. I
thought you could use 2>&1 to do this. Such as:

system("mycommand 2>&1 > results.log");

But all I get in the logfile is stdout. stderr still goes to the
screen.

Anyone have any clues?
 
P

Paul Lalli

I wrote a perl script which invokes system calls using system().

I want to be able to redirect the output of these calls to a file. I
thought you could use 2>&1 to do this. Such as:

system("mycommand 2>&1 > results.log");

But all I get in the logfile is stdout. stderr still goes to the
screen.

Anyone have any clues?

What version of Windows? That's mildly important.

On Windows 2000, what you had above gives me the same results. If you
switch the arguments, however, like so:

system("mycommand >results.log 2>&1");

I get both STDOUT and STDERR messages in results.log. Annoyingly,
however, all the STDERR prints are made before the STDOUT prints. I'm
willing to bet that's a buffer setting that can be fiddled with though.
I'll leave that as an excercise to the reader. :)

Paul Lalli
 
T

Toni Erdmann

Paul said:
What version of Windows? That's mildly important.

On Windows 2000, what you had above gives me the same results. If you
switch the arguments, however, like so:

system("mycommand >results.log 2>&1");

I get both STDOUT and STDERR messages in results.log. Annoyingly,
however, all the STDERR prints are made before the STDOUT prints. I'm
willing to bet that's a buffer setting that can be fiddled with though.
I'll leave that as an excercise to the reader. :)

That's normal behaviour.

mycommand 2>&1 > result.log

1.) redirect STDERR to the *current* STDOUT setting/file descriptor
2.) redirect STDOUT to a file (leaving STDERR as is == screen)

It does what you want.

mycommand > result.log 2>&1

1.) redirect STDOUT to a file
2.) redirect STDERR to the *current* STDOUT setting/file descriptor

Toni
 
B

Brian

I am using Windows2000. Ah, I did not know precisely what was
happening with the use of 2>&1, nor did I know you could place it at
the end. I have been using it with unix and I guess the behavior is
different.

Thanks for the help! Much appreciated!
 
T

Toni Erdmann

Brian said:
I am using Windows2000. Ah, I did not know precisely what was
happening with the use of 2>&1, nor did I know you could place it at
the end. I have been using it with unix and I guess the behavior is
different.

Thanks for the help! Much appreciated!

Yepp, it depends on the shell. And cmd.exe might behave different.
That's why I use 'cygwin' on Windows. That gives me 'bash' and all
the nice GNU tools.

Toni
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top