VMS-to-Unix Phrase Book
<=  Return           

3.7  Collecting STDOUT and STDERR

Problem

You want to be able to collect STDOUT and/or STDERR text for logging purposes.

Solution

Use I/O direction to create your own log files.

Discussion

In VMS, the batch job controller will automatically create a log file containing all diagnostic messages generated by a DCL command procedure and the commands it executes. The Unix cron, at, and batch commands are used for running programs and scripts in batch. By default these batch utilities want to send e-mail instead of creating a log file. Basically anything that would be sent to the terminal is mailed to the job's owner instead. Knowing this, you can control what is sent via e-mail as well as creating your own log files by using I/O redirection.

Consider the following script where myJob.pl is a simple Perl program that writes a labeled line to both STDOUT and STDERR.

     #!/bin/sh
     #
     #        File:     cases.sh
     #    Abstract:     Demo different types of redirection.
     #
     #=========
     
     #---------------------------------------+
     # STDOUT & STDERR to terminal.          |
     #---------------------------------------+
     
     ./myJob.pl "CASE 1"
     
     #---------------------------------------+
     # STDOUT to log.                        |
     #---------------------------------------+
     
     ./myJob.pl "CASE 2" >case.2
     
     #---------------------------------------+
     # STDERR to log.                        |
     #---------------------------------------+
     
     ./myJob.pl "CASE 3" 2>case.3
     
     #---------------------------------------+
     # STDOUT & STDERR to same log.          |
     #---------------------------------------|
     # The order is important!  Establish    |
     # the file first, *then* connect STDERR |
     # to the  same location as STDOUT.      |
     #---------------------------------------+
     
     ./myJob.pl "CASE 4" >case.4 2>&1
     
     #---------------------------------------+
     # STDOUT to log, pipe STDERR to another |
     # program.                              |
     #---------------------------------------|
     # Swap STDOUT and STDERR, now errors    |
     # are coming out STDOUT instead and     |
     # thus can be piped.                    |
     #---------------------------------------+
     
     ./myJob.pl "CASE 5" 3>&2 2>&1 1>&3 >case.5 | cat -net
     
     #---------------------------------------+
     # STDOUT & STDERR to same log *and*     | "Multipexing multiple I/O streams
     # STDERR to terminal.                   |  is never a pretty picture."
     #---------------------------------------|  The Perl Cookbook.
     # Label the data lines and take *all*   |
     # output and send it to  STDOUT.  Pipe  |
     # result to tee to save all in a file,  |
     # then filter tee's STDOUT to show only |
     # the STDERR lines.  The errors will    |
     # appear first in the log.              |
     #---------------------------------------+
     
     (./myJob.pl "CASE 6" | sed -e 's/^/DATA: /' ) 2>&1 \
       | tee case.6 \
       | grep -v '^DATA: '
     
     #==[ EOF: cases.sh ]==
Execution of this script sends the following output to the terminal:
unix> ./cases.sh
[CASE 1]: this is stdout
[CASE 1]: THIS IS STDERR
[CASE 2]: THIS IS STDERR
[CASE 3]: this is stdout
     1  [CASE 5]: THIS IS STDERR$
[CASE 6]: THIS IS STDERR
unix>
And the following files are created:
unix> ls -la case.*
-rw-rw----   1 wfc      users         25 Mar 13 23:20 case.2
-rw-rw----   1 wfc      users         25 Mar 13 23:20 case.3
-rw-rw----   1 wfc      users         50 Mar 13 23:20 case.4
-rw-rw----   1 wfc      users         25 Mar 13 23:20 case.5
-rw-rw----   1 wfc      users         56 Mar 13 23:20 case.6
unix>
unix> cat case.2
[CASE 2]: this is stdout
unix> cat case.3
[CASE 3]: THIS IS STDERR
unix> cat case.4
[CASE 4]: THIS IS STDERR
[CASE 4]: this is stdout
unix> cat case.5
[CASE 5]: this is stdout
unix> cat case.6
[CASE 6]: THIS IS STDERR
DATA: [CASE 6]: this is stdout
unix> 

See Also

3.2 - Using STDERR and Exit Codes;
Chapter 2 of Unix for OpenVMS Users ;
Chapter 47 of Unix Power Tools .


<=  Return           

Colophon:
Best Viewed With Any Browser
This page maintained by:
    Bill.Costa@unh.edu
    of the Enterprise Computing Group
    in the dept of Computing & Information Sevices
    at the University of New Hampshire

Typographical
Conventions

Created:  31-Jan-2001 BC
Revised:  11-Apr-2001 BC