[ Previous | Next | Contents | Glossary | Home | Search ]
AIX Version 4.3 System User's Guide: Operating System and Devices

Input and Output Redirection in the Korn Shell or POSIX Shell

Before the Korn shell executes a command, it scans the command line for redirection characters. These special notations direct the shell to redirect input and output. Redirection characters can appear anywhere in a simple command or can precede or follow a command. They are not passed on to the invoked command.

The shell performs command and parameter substitution before using the Word or Digit parameter except as noted. File name substitution occurs only if the pattern matches a single file and blank interpretation is not performed.

<Word Uses the file specified by the Word parameter as standard input (file descriptor 0).
>Word Uses the file specified by the Word parameter as standard output (file descriptor 1). If the file does not exist, the shell creates it. If the file exists and the noclobber option is on, an error results; otherwise, the file is truncated to zero length.
>|Word Same as the >Word command, except that this redirection statement overrides the noclobber option.
> >Word Uses the file specified by the Word parameter as standard output. If the file currently exists, the shell appends the output to it (by first seeking the end-of-file character). If the file does not exist, the shell creates it.
<>Word Opens the file specified by the Word parameter for reading and writing as standard input.
<<[-]Word Reads each line of shell input until it locates a line containing only the value of the Word parameter or an end-of-file character. The shell does not perform parameter substitution, command substitution, or file name substitution on the file specified. The resulting document, called a here document, becomes the standard input. If any character of the Word parameter is quoted, no interpretation is placed upon the characters of the document.

The here document is treated as a single word that begins after the next new-line character and continues until there is a line containing only the delimiter, with no trailing blank characters. Then the next here document, if any, starts. The format is:

   here document

If any character in word is quoted, the delimiter is formed by removing the quote on word. The here document lines will not be expanded. Otherwise, the delimiter is the word itself. If no characters in word are quoted, all lines of the here document will be expanded for parameter expansion, command substitution, and arithmetic expansion.

The shell performs parameter substitution for the redirected data. To prevent the shell from interpreting the \ (backslash), $ (dollar sign), and ` (single quote) characters and the first character of the Word parameter, precede the characters with a \ character.

If a - (minus sign) is appended to <<, the shell strips all leading tabs from the Word parameter and the document.

<&Digit Duplicates standard input from the file descriptor specified by the Digit parameter.
>& Digit Duplicates standard output in the file descriptor specified by the Digit parameter.
<&- Closes standard input.
>&- Closes standard output.
<&p Moves input from the coprocess to standard input.
>&p Moves output to the coprocess to standard output.

If one of these redirection options is preceded by a digit, then the file descriptor number referred to is specified by the digit (instead of the default 0 or 1). In the following example, the shell opens file descriptor 2 for writing as a duplicate of file descriptor 1:

... 2>&1

The order in which redirections are specified is significant. The shell evaluates each redirection in terms of the (FileDescriptor, File) association at the time of evaluation. For example, in the statement:

... 1>File 2>&1

the file descriptor 1 is associated with the file specified by the File parameter. The shell associates file descriptor 2 with the file associated with file descriptor 1 (File). If the order of redirections were reversed, file descriptor 2 would be associated with the terminal (assuming file descriptor 1 had previously been) and file descriptor 1 would be associated with the file specified by the File parameter.

If a command is followed by an & (ampersand) and job control is not active, the default standard input for the command is the empty file, /dev/null. Otherwise, the environment for the execution of a command contains the file descriptors of the invoking shell as modified by input and output specifications.

For more information about redirection, see "Input and Output Redirection"

Coprocess Facility

The Korn shell, or POSIX shell, allows you to run one or more commands as background processes. These commands, run from within a shell script, are called coprocesses. Coprocesses are useful when you want to communicate with a program.

Designate a coprocess by placing the |& (pipe, ampersand) operator after a command. Both standard input and output of the command are piped to your script.

A coprocess must meet the following restrictions:

The following example demonstrates how input is passed to and returned from a coprocess:

echo "Initial process"
./FileB.sh |&
read -p a b c d
echo "Read from coprocess: $a $b $c $d"
print -p "Passed to the coprocess"
read -p a b c d
echo "Passed back from coprocess: $a $b $c $d"
   echo "The coprocess is running"
   read a b c d
   echo $a $b $c $d

The resulting standard output is:

Initial process
Read from coprocess: The coprocess is running
Passed back from coprocess: Passed to the coprocess

The print -p command lets you write to the coprocess. To read from the coprocess, issue the read -p command.

Redirecting Coprocess Input and Output

The standard input and output of a coprocess is reassigned to a numbered file descriptor by using I/O redirection. For example, the command:

exec 5>&p

moves the input of the coprocess to file descriptor 5.

Once this is done, you can use standard redirection syntax to redirect command output to the coprocess. You can also start another coprocess. Output from both coprocesses is connected to the same pipe and is read with the read -p command. To stop the coprocess, type:

read -u5

[ Previous | Next | Contents | Glossary | Home | Search ]