Redirecting standard error messages to a file – HP NonStop G-Series User Manual

Page 137

Advertising
background image

$ ls >> file

If file does not exist, the shell creates it.

Redirecting Standard Error Messages to a File

When a command executes successfully, it displays the results on the standard output file. When
a command executes unsuccessfully, it displays error messages on the default standard error file:
the screen. However, the shell allows you to redirect the standard error file of a process from the
screen to a file.

The general format for standard error message redirection is as follows:

command 2> errorfile

The command entry is an OSS command. The errorfile entry is the name of the file to which
the process should write the standard error file. The 2> is a file descriptor digit combined with the
output redirection symbol. The file descriptor digit tells the shell which standard file to access so
that its contents can be redirected. The file descriptor digit 2 indicates that the standard error file
is being redirected.

OSS shell file descriptors are as follows:

File descriptor 0 (same as <) specifies the standard input file (the keyboard).

File descriptor 1 (same as >) specifies the standard output file (the screen).

File descriptor 2 specifies the standard error file (the screen).

In the following example, an error message is redirected to the file error when the ls command
attempts to display the nonexistent file reportx. The contents of file error are then displayed:

$ ls reportx 2> error

$ cat error

reportx not found

Although only the standard error file is redirected to a file in the preceding example, typically you
would redirect both the standard error file and the standard output file. See the next subsection,

“Redirecting Both Standard Error Messages and Standard Output” (page 137)

, for more information.

For many commands, the difference between the standard output and standard error files is difficult
to see. For instance, if you use the ls command to display a nonexistent file, an error message
displays on the screen. If you redirect the error message to a file as in the previous example, the
output is identical.

Redirecting Both Standard Error Messages and Standard Output

You may want to redirect both the standard output and standard error files at the same time. You
can redirect the standard output and standard error files to different files or to the same file.

The general format for redirecting both the standard output and standard error files to different
files is as follows:

command > outfile 2> errorfile

The command entry is an OSS command. The outfile entry is the file to which the process writes
the standard output file. The 2> symbol redirects the standard error output. The errorfile entry
is the file to which the process writes the standard error output.

The general format for redirecting both the standard output and standard error files to the same
file is the following:

command 1> outfile 2>&1

The command entry is an OSS command. The 1> symbol redirects the standard output file. The
outfile

entry is the file to which the process writes the standard output. The 2>&1 symbol tells

Understanding Standard Input, Output, and Error Messages

137

Advertising
This manual is related to the following products: