Skip to main content

How to read or write to a pipe?

  • February 15, 2013
  • 0 replies
  • 0 views

Problem:

The ability to pipe input and output between commands is a powerful feature of Unix.  Can Cobol programs pipe to or from Unix commands and/or other Cobol programs?

Resolution:

Yes.   The way in which a pipe file name is specified is documented in programming guide manual "File Handling", chapter "Filenames", section "Setting up Pipes".

The pipe direction (in, out, or bidirectional) character, the name of the other command, and that command's command line arguments may be specified as a literal in the SELECT statement or as the value of the DD_assign-name environment variable.  Examples:

  select infile assign to '<cat $COBDIR/etc/cobver'

      organization is line sequential.

*The "<" character defines an input pipe to the program.

  select infile assign to external filespec

      organization is line sequential.

In this case, environment variable "DD_FILESPEC" must be set to a value similar in structure to that shown in the literal in the first example:

  export DD_FILESPEC="<cat $COBDIR/etc/cobver"

Note that the enclosing double quotes are  mandatory and for two reasons:

1. To protect the "<" character from shell interpretation as part of the export command

2. The value of the variable necessarily contains spaces which separate the command name

      from its command line arguments.  The spaces must be protected from shell interpretation

      as part of the export command.

The attached tar file contains a Cobol source and two scripts.  To run the demo:

binary FTP the tar file to a Unix system on which Server Express (any version) is installed.

tar -xvf pipedemo.tar

./pipedemo

The output will be the PRN (Product Reference Number) of the Cobol product used.

The pipedemo script compiles the pipeit program, sets up two external pipes, and runs pipeit.  Program "pipeit" receives input from the "cat" command through an input pipe and outputs it to the "grep" command through an output pipe.

The catpipe script runs the "cat" command.  It is there simply to show that the piped command can be the name of a shell script.  Script "catpipe" is unnecessary if, in the pipedemo script, the commented-out "export" command is made active and the active one commented out as below:

  export DD_INFILE="< cat $COBDIR/etc/cobver"

#  export DD_INFILE="< ./catpipe"

Attachments:

pipedemo.tar

Old KB# 4551