Previous Section
 < Day Day Up > 
Next Section


Standard Input/Output and Redirection

The data in input and output operations is organized like a file. Data input at the keyboard is placed in a data stream arranged as a continuous set of bytes. Data output from a command or program is also placed in a data stream and arranged as a continuous set of bytes. This input data stream is referred to in Linux as the standard input, while the output data stream is called the standard output. There is also a separate output data stream reserved solely for error messages, called the standard error (see the section on the standard error later in this chapter).

Because the standard input and standard output have the same organization as that of a file, they can easily interact with files. Linux has a redirection capability that lets you easily move data in and out of files. You can redirect the standard output so that, instead of displaying the output on a screen, you can save it in a file. You can also redirect the standard input away from the keyboard to a file, so that input is read from a file instead of from your keyboard.

When a Linux command is executed that produces output, this output is placed in the standard output data stream. The default destination for the standard output data stream is a device—in this case, the screen. Devices, such as the keyboard and screen, are treated as files. They receive and send out streams of bytes with the same organization as that of a byte-stream file. The screen is a device that displays a continuous stream of bytes. By default, the standard output will send its data to the screen device, which will then display the data.

For example, the ls command generates a list of all filenames and outputs this list to the standard output. Next, this stream of bytes in the standard output is directed to the screen device. The list of filenames is then printed on the screen. The cat command also sends output to the standard output. The contents of a file are copied to the standard output whose default destination is the screen. The contents of the file are then displayed on the screen.

Redirecting the Standard Output: > and >>

Suppose that instead of displaying a list of files on the screen, you would like to save this list in a file. In other words, you would like to direct the standard output to a file rather than the screen. To do this, you place the output redirection operator, > (greater-than sign), and the name of a file on the command line after the Linux command. Table 8-4 lists the different ways you can use the redirection operators. In the next example, the output of the ls command is redirected from the screen device to a file:

Table 8-4: The Shell Operations

Command

Execution

ENTER

Execute a command line.

;

Separate commands on the same command line.

command\
opts args

Enter backslash before carriage return to continue entering a command on the next line.

'command'

Execute a command.

$(command)

Execute a command.

Special Characters
for Filename Expansion

Execution

*

Match on any set of characters.

?

Match on any single characters.

[]

Match on a class of possible characters.

\

Quote the following character. Used to quote special characters.

Redirection

Execution

command > filename

Redirect the standard output to a file or device, creating the file if it does not exist and overwriting the file if it does exist.

command < filename

Redirect the standard input from a file or device to a program.

command >> filename

Redirect the standard output to a file or device, appending the output to the end of the file.

command >! filename

In the C shell and the Korn shell, the exclamation point forces the overwriting of a file if it already exists. This overrides the noclobber option.

command 2> filename

Redirect the standard error to a file or device in the Bourne shell.

command 2>> filename

Redirect and append the standard error to a file or device in the Bourne shell.

command 2>&1

Redirect the standard error to the standard output in the Bourne shell.

command >& filename

Redirect the standard error to a file or device in the C shell.

Pipes

Execution

command | command

Pipe the standard output of one command as input for another command.

command |& command

Pipe the standard error as input to another command in the C shell.

$ ls -l *.c > programlist

The redirection operation creates the new destination file. If the file already exists, it will be overwritten with the data in the standard output. You can set the noclobber feature to prevent overwriting an existing file with the redirection operation. In this case, the redirection operation on an existing file will fail. You can overcome the noclobber feature by placing an exclamation point after the redirection operator. You can place the noclobber command in a shell configuration file to make it an automatic default operation (see Chapter 10). The next example sets the noclobber feature for the BASH shell and then forces the overwriting of the oldletter file if it already exists:

$ set -o noclobber
$ cat myletter >! oldletter

Although the redirection operator and the filename are placed after the command, the redirection operation is not executed after the command. In fact, it is executed before the command. The redirection operation creates the file and sets up the redirection before it receives any data from the standard output. If the file already exists, it will be destroyed and replaced by a file of the same name. In effect, the command generating the output is executed only after the redirected file has been created.

In the next example, the output of the ls command is redirected from the screen device to a file. First the ls command lists files, and in the next command, ls redirects its file list to the listf file. Then the cat command displays the list of files saved in listf. Notice the list of files in listf includes the listf filename. The list of filenames generated by the ls command includes the name of the file created by the redirection operation—in this case, listf. The listf file is first created by the redirection operation, and then the ls command lists it along with other files. This file list output by ls is then redirected to the listf file, instead of being printed on the screen.

$ ls
mydata intro preface
$ ls > listf
$ cat listf
mydata intro listf preface
Tip 

Errors occur when you try to use the same filename for both an input file for the command and the redirected destination file. In this case, because the redirection operation is executed first, the input file, because it exists, is destroyed and replaced by a file of the same name. When the command is executed, it finds an input file that is empty.

You can also append the standard output to an existing file using the >> redirection operator. Instead of overwriting the file, the data in the standard output is added at the end of the file. In the next example, the myletter and oldletter files are appended to the alletters file. The alletters file will then contain the contents of both myletter and oldletter.

$ cat myletter >> alletters
$ cat oldletter >> alletters

The Standard Input

Many Linux commands can receive data from the standard input. The standard input itself receives data from a device or a file. The default device for the standard input is the keyboard. Characters typed on the keyboard are placed in the standard input, which is then directed to the Linux command. Just as with the standard output, you can also redirect the standard input, receiving input from a file rather than the keyboard. The operator for redirecting the standard input is the less-than sign, <. In the next example, the standard input is redirected to receive input from the myletter file, rather than the keyboard device. The contents of myletter are read into the standard input by the redirection operation. Then the cat command reads the standard input and displays the contents of myletter.

$ cat < myletter
hello Christopher
How are you today
$

You can combine the redirection operations for both standard input and standard output. In the next example, the cat command has no filename arguments. Without filename arguments, the cat command receives input from the standard input and sends output to the standard output. However, the standard input has been redirected to receive its data from a file, while the standard output has been redirected to place its data in a file.

$ cat < myletter > newletter


Previous Section
 < Day Day Up > 
Next Section
This HTML Help has been published using the chm2web software.