redirect stderr and stdout to the same file 
Author Message
 redirect stderr and stdout to the same file

 I'm using perl on NT and here's my problem:

 I have a batch script that runs perl script which calls another perl
script which calls other programs (any executables or batch scripts).

cmd1.cmd --> perl --> perl --> other exe or bat or whatever.
 I want all the stdout and stderr from the second perl and all the
program to appear in one log file as they are printed.
I've tried to close STDERR and STDOUT and open them with redirection to
the log file:
open (STDOUT,">> $logfile");
open (STDERR,">> $logfile");

the problem that I have is that always the stderr appears before the
stdout, and the output of different programs appear together , and not
in between other messages:

in :
print STDOUT "before program A\n";
print STDOUT "after program A\n";
 (in A I have several prints to stdout and stderr)
the messages from A program will appear together , before the two
messages of the perl script.

Any help or suggetsion will be welcome

Sat, 02 Sep 2000 03:00:00 GMT  
 redirect stderr and stdout to the same file

The command line syntax you need is the same as the Unix idiom:

perl 1>outputfilename 2>&1

Use ">>" instead of ">" if you want to append rather than replace.

Here is an example:

perl 1>test.out 2>&1

where "" is a file containing the following line


and "" is a file containing the lines:

print STDOUT "This is a message to STDOUT\n";
print STDERR "This is a message to STDERR\n";

You should see both messages written to test.out

NOTE:  You have to specify "perl <scriptname>.pl", not just
"<scriptname>" if you want to use I/O redirection from the command
line.  This is a limitation of the way the NT command processor works.

NOTE2: This does not work at all on Windows 95.  So far as I know, you
can't redirect stderr from the command line (someone please correct me
if I'm wrong).

Phil Hanna
saspeh at unx dot sas dot com

Sat, 02 Sep 2000 03:00:00 GMT  
 [ 2 post ] 

 Relevant Pages 

1. assigning STDERR whild redirecting STDOUT to a file

2. Redirecting STDOUT/STDERR to a file

3. redirect stderr and stdout to same file

4. how to redirect stderr and stdout to same file

5. HELP - with redirect of STDERR and STDOUT

6. redirect STDERR to STDOUT ?????

7. redirecting stderr and stdout

8. writing to terminal even with STDOUT and STDERR redirected

9. Problem with redirecting STDOUT and STDERR

10. How to redirect STDOUT,STDERR and keep the message order

11. Redirect stderr to stdout w/ Perl under Unix?

12. Redirecting STDERR to STDOUT (on NT)


Powered by phpBB® Forum Software