capturing stderr from windows

Discussion in 'Perl Misc' started by Brian, Apr 19, 2004.

  1. Brian

    Brian Guest

    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?
     
    Brian, Apr 19, 2004
    #1
    1. Advertising

  2. Brian

    Paul Lalli Guest

    On Mon, 19 Apr 2004, Brian wrote:

    > 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
     
    Paul Lalli, Apr 19, 2004
    #2
    1. Advertising

  3. Brian

    Toni Erdmann Guest

    Paul Lalli wrote:

    > On Mon, 19 Apr 2004, Brian wrote:
    >
    >
    >>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. :)
    >


    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
     
    Toni Erdmann, Apr 19, 2004
    #3
  4. Brian

    Brian Guest

    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!
     
    Brian, Apr 21, 2004
    #4
  5. Brian

    Toni Erdmann Guest

    Brian wrote:
    > 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
     
    Toni Erdmann, Apr 22, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doru-Catalin Togea

    Capturing output from stderr

    Doru-Catalin Togea, Dec 5, 2005, in forum: Python
    Replies:
    2
    Views:
    383
    Doru-Catalin Togea
    Dec 5, 2005
  2. Fuzzyman
    Replies:
    3
    Views:
    645
    Gabriel Genellina
    Jan 7, 2007
  3. Carl Lerche

    Capturing stderr from Kernel.exec

    Carl Lerche, Sep 9, 2006, in forum: Ruby
    Replies:
    2
    Views:
    130
  4. Ted Flethuseo

    capturing stderr for a file

    Ted Flethuseo, Feb 17, 2011, in forum: Ruby
    Replies:
    1
    Views:
    108
    Avdi Grimm
    Feb 17, 2011
  5. Mike

    capturing stdout and stderr?

    Mike, Aug 9, 2009, in forum: Perl Misc
    Replies:
    0
    Views:
    102
Loading...

Share This Page