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

Standard Input, Standard Output, and Standard Error

When a command begins running, it usually expects that three files are already open: standard input, standard output, and standard error (sometimes called error output or diagnostic output). A number, called a file descriptor, is associated with each of these files, as follows:

File descriptor 0 Standard input
File descriptor 1 Standard output
File descriptor 2 Standard error (diagnostic) output

A child process normally inherits these files from its parent. All three files are initially assigned to the workstation (0 to the keyboard, 1 and 2 to the display). The shell permits them to be redirected elsewhere before control is passed to a command.

When you enter a command, if no file name is given, your keyboard is the standard input, sometimes denoted as stdin. When a command finishes, the results are displayed on your screen.

Your screen is the standard output, sometimes denoted as stdout. By default, commands take input from the standard input and send the results to standard output.

Standard error, sometimes denoted as stderr, is where error messages go. By default, this is your screen.

These default actions of input and output can be varied. You can use a file as input and write results of a command to a file. This is called input/output redirection, which is one of the powerful features of a UNIX operating system.

The output from a command, which normally goes to the terminal, can easily be redirected to a file instead. This is known as output redirection. This is useful when you have a lot of output that is difficult to read on the screen or when you want to put files together to create a larger file.

Though not used as much as output redirection, the input for a command, which normally comes from the keyboard, can also be redirected from a file. This is known as input redirection. Redirection of input lets you prepare a file in advance and then have the command read the file.

Redirecting Standard Output

When the notation > filename is added to the end of a command, the output of the command is written to the specified file name. The > symbol is known as the output redirection operator.

Any command that outputs its results to the screen can have its output sent to a file.

Redirecting Output to a File

For example, to send the results of the who command to a file called users, enter:

who > users
Note: If the file users already exists, it is written over, unless the noclobber option of the set built-in ksh (Korn shell) or csh (C shell) command is specified.

To see the contents of the file users, enter:

cat users

A list similar to the following appears:

denise    lft/0 May 13 08:05
marta     pts/1 May 13 08:10
endrica   pts/2 May 13 09:33

For example, to send the current directory listing to a file, enter:

ls > dirlist

Redirecting Output and Appending It to a File

When the notation > > filename is added to the end of a command, the output of the command is appended to the specified file name rather than writing over any existing data. The > > symbol is known as the append redirection operator.

For example, to append file2 to file1, enter:

cat file2 > > file1
Note: If the file file1 does not exist, it is created, unless the noclobber option of the set built-in ksh (Korn shell) or csh (C shell) command is specified.

Creating a Text File with Redirection from the Keyboard

The cat command alone takes whatever you enter at the keyboard as input. You can redirect this input to a file. Enter Ctrl-D on a new line to signal the end of the text.

At the system prompt, enter:

cat > filename
This is a test.
^D

Concatenating (Join) Text Files

Combining various files into one file is known as concatenation.

For example, at the system prompt, enter:

cat file1 file2 file3 > file4

The previous example creates file4, which consists of file1, file2, and file3 appended in the order given.

The following example shows a common error when concatenating files:

cat file1 file2 file3 > file1
Attention: In this example, you may think the cat command will append the contents of file1, file2, and file3 into file1. The cat command creates the output file first, so it actually erases the contents of file1 and then appends file2 and file3 to it.

Redirecting Standard Input

When the notation < filename is added to the end of a command, the input of the command is read from the specified file name. The < symbol is known as the input redirection operator.

Note: Only commands that normally take their input from the keyboard can have their input redirected.

For example, to send the file letter1 as a message to user denise with the mail command, enter:

mail denise < letter1

Discarding Output with the /dev/null File

The /dev/null file is a special file. This file has a unique property; it is always empty. Any data you send to /dev/null is discarded. This is a useful feature when you run a program or command that generates output you want to ignore.

For example, you have a program named myprog that accepts input from the screen and generates messages while it is running that you would rather ignore. To read input from the file myscript and discard the standard output messages, enter:

myprog < myscript >/dev/null

In this example, myprog uses the file myscript as input and all standard output is discarded.

Redirecting Standard Error and Other Output

In addition to the standard input and standard output, commands often produce other types of output, such as error or status messages known as diagnostic output. Like standard output, standard error output is written to the screen unless redirected.

Generally, when a command starts, three files are already open: stdin (standard input), stdout (standard output), and stderr (standard error). If you want to redirect standard input or standard output, you can use the <, >, or > > symbols. However, if you want to redirect standard error or other output, you must use a file descriptor. File descriptors can also be specified to redirect standard input and standard output, but are already the default values.

A file descriptor is a number associated with each of the I/O files a command ordinarily uses. The following numbers are associated with standard input, output, and error:

0 Standard input (keyboard)
1 Standard output (display)
2 Standard error (display)

To redirect standard error output, type the file descriptor number 2 in front of the output or append redirection symbols (> or > >) and a file name after the symbol. For example, the following command takes the standard error output from the cc command where it is used to compile testfile.c and appends it to the end of the ERRORS file:

cc testfile.c 2 > > ERRORS

Other types of output can also be redirected using the file descriptors from 0 through 9. For example, if the cmd command writes output to file descriptor 9, you can redirect that output to the savedata file with the following command:

cmd 9> savedata

If a command writes to more than one output, you can independently redirect each one. Suppose that a command directs its standard output to file descriptor 1, directs its standard error output to file descriptor 2, and builds a data file on file descriptor 9. The following command line redirects each of these outputs to a different file:

command > standard 2> error 9> data

Inline Input (Here) Documents

A command in the form of:

command << eofstring

in which eofstring is any string that does not contain pattern-matching characters, the shell takes the subsequent lines as the standard input of command until the shell reads a line consisting of only eofstring (possibly preceded by one or more tab characters). The lines between the first eofstring and the second are frequently referred to as an inline input, or here, document. If a - (minus) immediately follows the << redirection characters, the shell strips leading tab characters from each line of the here document before it passes the line to the command.

The shell creates a temporary file containing the here document and performs variable and command substitution on the contents before passing the file to the command. It performs pattern matching on file names that are part of command lines in command substitutions. To prohibit all substitutions, quote any character of the eofstring:

command << \eofstring

The here document is especially useful for a small amount of input data that is more conveniently placed in the shell procedure rather than kept in a separate file (such as editor scripts). For instance, you could enter:

cat <<- xyz   
   This message will be shown on the 
   display with leading tabs removed.
   xyz

This feature is most useful in shell procedures.

Pipes and Filters

UNIX lets you connect two or more commands in such a way that the standard output of one command is used as the standard input of another command. A set of commands connected this way is known as a pipeline. The connection that joins the commands is known as a pipe. Pipes are another important feature of UNIX because they let you tie many single-purpose commands into one powerful command.

You can direct the output from one command to become the input for another command using a pipeline. The commands are connected by a | (pipe) symbol.

When a command takes its input from another command, modifies it, and sends its results to standard output, it is known as a filter. Filters can be used alone but they are especially useful in pipelines. The most common filters are:

For example, the ls command writes the contents of the current directory to the screen in one scrolling data stream. When more than one screen of information is presented, some data is lost from view. To control the output so the contents display screen by screen, you can use a pipeline to direct the output of the ls command to the pg command, which controls the format of output to the screen as shown in the following example:

ls | pg

In the example, the output of the ls command is the input for the pg command. Press Enter to continue to the next screen.

Pipelines operate in one direction only (left to right). Each command in a pipeline runs as a separate process and all processes can run at the same time. A process pauses when it has no input to read or when the pipe to the next process is full.

Another example of using pipes is with the grep command. grep searches a file for lines that contain strings of a certain pattern. To display all your files created or modified in July, enter:

ls -l | grep Jul

In the example, the output of the ls command is the input for the grep command.

Displaying Program Output and Copying It to a File (tee command)

The tee command, used with a pipe, reads standard input, then writes the output of a program to standard output and simultaneously copies it into the specified file or files. This gives you the advantage of viewing your output immediately and storing it for future use at the same time.

For example, to view and save the output from a command at the same time, enter:

ps -ef | tee program.ps

This displays the standard output of the command ps -ef at the work station, and at the same time saves a copy of it in the file program.ps. If program.ps already exists, it is deleted and replaced, unless the noclobber option of the set built-in command is specified.

For example, to view and save the output from a command to an existing file:

ls -l | tee -a program.ls

This displays the standard output of ls -l at the workstation and at the same time appends a copy of it to the end of program.ls. If the file program.ls does not exist, it is created, unless the noclobber option of the set built-in command is specified.

The system displays information similar to the following, and the program.ls file contains the same information:

-rw-rw-rw-   1 jones   staff   2301   Sep 19    08:53 161414
-rw-rw-rw-   1 jones   staff   6317   Aug 31    13:17 def.rpt
-rw-rw-rw-   1 jones   staff   5550   Sep 10    14:13 try.doc

See the tee command in the AIX Version 4.3 Commands Reference for the exact syntax.

Clearing Your Screen (clear Command)

You can empty the screen of messages and keyboard input with the clear command.

At the prompt, enter:

clear

The system clears the screen and displays the prompt.

See the clear command in the AIX Version 4.3 Commands Reference for the exact syntax.

Sending a Message to Standard Output (echo Command)

You can display messages on the screen with the echo command.

For example, to write a message to standard output, at the prompt, enter:

echo Please insert diskette . . .

The system displays the following:

Please insert diskette . . .

For example, to use the echo command with pattern-matching characters, at the prompt, enter:

echo The back-up files are: *.bak

The system displays the message The back-up files are: followed by the file names in the current directory ending with .bak.

See the echo command in the AIX Version 4.3 Commands Reference for the exact syntax.

Appending a Single Line of Text to a File (echo Command)

You can add a single line of text to a file with the echo command, used with the append redirection operator.

For example, at the prompt, enter:

echo Remember to backup mail files by end of week.>
 
>notes

This adds the message Remember to backup mail files by end of week. to the end of the file notes.

See the echo command in the AIX Version 4.3 Commands Reference for the exact syntax.

See the echo command in the AIX Version 4.3 Commands Reference Book for more information and the exact syntax.

Copying Your Screen to a File (capture and script Commands)

You can copy everything printed on your terminal to a file that you specify with the capture command, which emulates a VT100 terminal.

The script command also lets you copy everything printed on your terminal to a file that you specify, without emulating a VT100 terminal.

Both commands are useful for producing hardcopy records of terminal dialogs.

For example, to capture the screen of a terminal while emulating a VT100, at the prompt, enter:

capture screen.01

The system displays information similar to the following:

Capture command is started. The file is screen.01.
Use ^P to dump screen to file screen.01.
You are now emulating a vt100 terminal.
Press Any Key to continue.

After entering data and dumping the screen contents, stop the capture command by pressing Ctrl-D or entering exit. The system displays information similar to the following:

Capture command is complete. The file is screen.01.
You are NO LONGER emulating a vt100 terminal.

Use the cat command to display the contents of your file.

For example, to capture the screen of a terminal, at the prompt, enter:

script

The system displays information similar to the following:

Script command is started. The file is typescript.

Everything displayed on the screen is now copied to the file typescript.

To stop the script command, press Ctrl-D or enter exit. The system displays information similar to the following:

Script command is complete. The file is typescript.

Use the cat command to display the contents of your file.

See the capture and script commands in the AIX Version 4.3 Commands Reference for the exact syntax.

Displaying Text in Large Letters on Your Screen (banner Command)

The banner command displays ASCII characters to your screen in large letters. Each line in the output can be up to 10 digits or uppercase or lowercase characters in length.

For example, at the prompt, enter:

banner GOODBYE!

The system displays GOODBYE! in large letters at your screen.

See the banner command in theAIX Version 4.3 Commands Reference for the exact syntax.

Related Information

Commands Overview

Processes Overview

Shells Overview

Korn Shell or POSIX Shell

Bourne Shell

C Shell

Files Overview

The null special file.


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