in reply to Re: Re: Can't spawn "cmd.exe": No error at
in thread Can't spawn "cmd.exe": No error at

2&>error_file is not DOS/WindowsCMD/WhateverSoftCommandPrompt's syntax, it is 2>error_file
2&>error_file is (I luv) Unix.
you should always be careful about pasting code that uses system calls, as it is very different between OS'es.
  • Comment on Re: Re: Re: Can't spawn "cmd.exe": No error at

Replies are listed 'Best First'.
Re: Re: Re: Re: Can't spawn "cmd.exe": No error at
by BrowserUk (Patriarch) on Jul 18, 2003 at 00:25 UTC

    This "advice" is wrong. Windows CMD does support 2>&1 and has since at least NT3.51 if not before.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

      One final bit... I have a file handle open for a file trace.log. Is there any way to append the output from the system operation to that file? Well, other than the obvious of closing the trace file, performing the system operation, then re-opening the trace file.

        No way that I know of, but closing the file and then re-opening for append either side of the system calls doesn't seem so onerous? Just make sure that you use the append form of redirection on your system commands and when re-opening the file in the calling program

        #! perl -slw use strict; open F, '>junk.log' or die $!; print F 'From calling program via F'; close F; system( 'perl -le"print q[This goes via STDOUT]; print STDERR q[This g +oes via STDERR];" 1>>junk.log 2>&1' ); open F, '>>junk.log' or die $!; print F 'From calling program via F'; close F; __END__ P:\test>type junk.log From calling program via F This goes via STDERR This goes via STDOUT From calling program via F

        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

Re: Re: Re: Re: Can't spawn "cmd.exe": No error at
by mgibian (Acolyte) on Jul 17, 2003 at 23:03 UTC
    To clarify, I am looking for the best way to capture all output from an arbitrary command run on windows via Perl with the maximum amount of error handling. My understanding is that system coupled with output redirection is the best way to do this, but I'd happily stand corrected if there's a better way. Otherwise, is:
    system("foo > stdoutfile 2> stdoutfile");
    the best way to capture all output from the command foo? Are there any buffer issues by having to specify the file twice rather than the Unix technique of copying the descriptor with 2>&1?

      There is nothing wrong with your syntax for redirecting STDOUT & STDERR to the same file.

      P:\test>perl -e"print 'This goes to stdout', $/; print STDERR 'This go +es to STDERR', $/;" 1>junk.log 2>&1 P:\test>type junk.log This goes to STDERR This goes to stdout

      One caveat is that in common with several other POSIX systems, you need to do this in the right order; that shown above. The other way around does not do the same thing.

      The other caveat, also common to many systems, is that depending upon the buffering stratecies used internally by the program you are running, you may find that the output from the two streams is not correctly interleaved. Often this shows up as STDERR output showing up before STDOUT lines, with the latter tending to come bunched together in blocks. This is because STDOUT os often buffered whereas STDERR is not. You'll notice in the above example the STDERR line precedes the STDOUT line.

      If the programs/commands in question are other perl scripts then you can correct this by disabling buffering on STDOUT, but if they are programs written in C (or other languages) to which you do not have the source, there is little or nothing you can do about this.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

      So now I am totally confused. I changed my code to use 1> stdoutfile 2> stdoutfile per the prior comment, and now I'm getting file being used by another process. If I use separate files for 1> and 2> it works. AND 2>&1 sure does seem to work?