[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

System User's Guide: Operating System and Devices

Standard Input, Standard Output, and Standard Error

When a command begins running, it usually expects that the following 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.

Error messages are directed to standard error, sometimes denoted as stderr. 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.

The output from a command, which normally goes to the display device, 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

The output of a process can be redirected to a file by typing the command followed by the file name. For example, to send the results of the who command to a file called users, type:

who > users

Press Enter.

Note: If the users file already exists, it is deleted and replaced, 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 users file , type:

cat users

Press Enter.

A list similar to the following displays:

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

Redirecting Output and Appending 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, type:

cat file2 >> file1

Press Enter.

Note: If the file1 file 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

Used alone, the cat command uses whatever you type 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, type:

cat > filename
This is a test.
^D

Concatenating Text Files

Combining various files into one file is known as concatenation.

For example, at the system prompt, type:

cat file1 file2 file3 > file4

Press Enter.

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 might expect the cat command to 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, type:

mail denise < letter1

Press Enter.

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, type:

myprog < myscript >/dev/null

Press Enter.

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.

If you want to redirect standard error or other output, you must use a file descriptor. A file descriptor is a number associated with each of the I/O files that a command ordinarily uses. File descriptors can also be specified to redirect standard input and standard output, but are already the default values. 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 the testfile.c file 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

Using Inline Input (Here) Documents

If a command is in the following form:

command << eofstring

and eofstring is any string that does not contain pattern-matching characters, then 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 hyphen (-) 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 type:

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

Press Enter.

Using Pipes and Filters

You can connect two or more commands so 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 useful 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 as follows:

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 lscommand 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. The grep command searches a file for lines that contain strings of a certain pattern. To display all your files created or modified in July, type:

ls -l | grep Jul

Press Enter.

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

Displaying Program Output and Copying 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. Use the tee command to view your output immediately and at the same time, store it for future use.

For example, type:

ps -ef | tee program.ps

Press Enter.

This displays the standard output of the ps -ef command at the display device, and at the same time saves a copy of it in the program.ps file. If the program.ps file 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 display device and at the same time appends a copy of it to the end of the program.ls file.

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 5L Version 5.2 Commands Reference for the complete syntax.

Clearing Your Screen (clear Command)

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

At the prompt, type:

clear

Press Enter.

The system clears the screen and displays the prompt.

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, type:

echo Please insert diskette . . .

Press Enter.

The system displays the following:

Please insert diskette . . .

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

echo The back-up files are: *.bak

Press Enter.

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

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, type:

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

Press Enter.

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

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.

You can use the script command to copy everything printed on your terminal to a file that you specify, without emulating a VT100 terminal.

Both commands are useful for printing records of terminal dialogs.

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

capture screen.01

Press Enter.

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 typing exit and pressing Enter. 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, type:

script

Press Enter.

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 typescript file.

To stop the script command, press Ctrl-D or type exit and press Enter. 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 5L Version 5.2 Commands Reference for the complete 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, type:

banner GOODBYE!

Press Enter.

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

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]