Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Associating Files with a File Descriptor

By default, the shell provides you with three standard file descriptors for every command. With it, you can also associate any file with file descriptors using the exec command.

Associating a file with a file description is useful when you need to redirect output or input to a file many times but you don’t want to repeat the filename several times.

To open a file for writing, use one of the following forms:

exec n>file
exec n>>file

Here n is an integer, and file is the name of the file you want to open for writing. The first form overwrites the specified file if it exists. The second form appends to the specified file. For example, the following command

$ exec 4>fd4.out

associates the file fd4.out with the file descriptor 4.

To open a file for reading, you use the following form:

exec n<file

Here n is an integer, and file is the name of the file you want to open for reading.

General Input/Output Redirection

You can perform general output redirection by combining a file descriptor and an output redirection operator. The general forms are

command n> file
command n>> file

Here command is the name of a command, such as ls, n is a file descriptor (integer), and file is the name of the file. The first form redirects the output of command to the specified file, whereas the second form appends the output of command to the specified file.

For example, you can write the standard output redirection forms in the general form as

command 1> file
command 1>> file

Here the 1 explicitly states that STDOUT is being redirected into the given file.

General input redirection is similar to general output redirection. It is performed as follows:

command n<file

Here command is the name of a command, such as ls, n is a file descriptor (integer), and file is the name of the file. For example, the standard input redirection forms can be written in the general form as

command 0<file

Redirecting STDOUT and STDERR to Separate Files

One of the most common uses of file descriptors is to redirect STDOUT and STDERR to separate files. The basic syntax is

command 1> file1 2> file2

Here the STDOUT of the specified command is redirected to file1, and the STDERR (error messages) is redirected to file2.

Often the STDOUT file descriptor, 1, is not written, so a shorter form of the basic syntax is

command > file1 2> file2

You can also use the append operator in place of either standard redirect operator:

command >> file1 2> file2
command > file1 2>> file2
command >> file1 2>> file2

The first form appends STDOUT to file1 and redirects STDERR to file2. The second form redirects STDOUT to file1 and appends STDERR to file2. The third form appends STDOUT to file1 and appends STDERR to file2.

In the following example, I will illustrate using form1 because you are interested in only the output of the command:

for FILE in $FILES
do
    ln -s $FILE ./docs >> /tmp/ln.log 2> /dev/null
done

Here the STDOUT of ln is appended to the file /tmp/ln.log, and the STDERR is redirected to the file /dev/null, in order to discard it.


Tip:  
The file /dev/null is a special file available on all UNIX systems used to discard output. It is sometimes referred to as the bit bucket.

If you redirect the output of a command into /dev/null, it is discarded. You see it used for this purpose often. For example, the command

rm file > /dev/null

discards the output of the rm command.

If you use cat to display the contents of /dev/null to a file, the file’s contents are erased:

$ cat /dev/null > file

After this command, the file still exists, but its size is zero.


Redirecting STDOUT and STDERR to the Same File

You looked at how to use file descriptors to redirect STDOUT and STDERR to different files, but sometimes you need to redirect both to the same file. In general, you do this by

command > file 2>&1
list > file 2>&1

Here STDOUT (file description 1) and STDERR (file descriptor 2) are redirected into the specified file.

Here is a situation where it is necessary to redirect both the standard output and the standard error:

rm -rf /tmp/my_tmp_dir > /dev/null 2>&1 ; mkdir /tmp/my_tmp_dir

Here, you are not interested in the error message or the informational message printed by the rm command. You only want to remove the directory, thus its output or any error message it prints are redirected to /dev/null.

If you had one command that should append its standard error and standard output to a file, you use the following form:

command >> file 2>&1
list >> file 2>&1

An example of a command that might require this is

rdate -s ntp.nasa.gov >> /var/log/rdate.log 2>&1

Here you are using the rdate command to synchronize the time of the local machine to an Internet time server and you want to keep a log of all the messages.

Printing a Message to STDOUT You can also use this form of output redirection to output error messages on STDERR. The basic syntax is

echo string 1>&2
printf format args 1>&2

You might also see these commands with the STDOUT file descriptor, 1, omitted:

echo string >&2
printf format args >&2

As an example, say that you need to display an error message if a directory is given instead of a file. You can use the following if statement:

if [ ! -f $FILE ] ; then echo  "ERROR: $FILE is not a file" >&2 ; fi


Previous Table of Contents Next