45.21 n>&m: Swap Standard Output and Standard ErrorBy default, a command's standard error goes to your terminal. The standard output goes to the terminal or is redirected somewhere (to a file, down a pipe, into backquotes). Sometimes you want the opposite. For instance, you may need to send a command's standard output to the screen and grab the error messages (standard error) with backquotes. Or, you might want to send a command's standard output to a file and the standard error down a pipe to an error-processing command. Here's how to do that in the Bourne shell. (The C shell can't do this.) File descriptors 0, 1, and 2 are, respectively, the standard input, standard output, and standard error (article 45.20 explains). Without redirection, they're all associated with the terminal file /dev/tty ( 45.20 ) . It's easy to redirect any descriptor to any file - if you know the filename. For instance, to redirect file descriptor 2 to errfile , type:
$ You know that a pipe and backquotes also redirect the standard output:
$
But there's no filename associated with the pipe or backquotes, so
you can't use the
Let's start slowly: by sending both standard output and standard error
to the pipe or backquotes.
The Bourne shell operator
$
In both those examples,
You can use more than one of those "Oh!" you might say, "To swap standard output and standard error - make stderr go down a pipe and stdout go to the screen-I could do this!"
$
Sorry, Charlie.
When the shell sees
This is one place the other file descriptors, 3 through 9, come in handy.
They normally aren't used.
You can use one of them as a "holding place," to remember where another
file descriptor "pointed."
For example, one way to read the operator We'll take that step-by-step below. The command line you want is one of these:
$
How does it work?
The next four figures
break the second command line (with the backquotes) into the same
steps the shell follows as it rearranges the file descriptors.
You can try these on your terminal, if you'd like.
Each figure adds another The figures use a grep command reading two files. afone is readable and grep finds one matching line in it; the line is written to the standard output. bfoen is misspelled and so is not readable; grep writes an error message to the standard error. In each figure, you'll see the terminal output (if any) just after the variable-setting command with the backquotes. The text grabbed by the backquotes goes into the shell variable; the echo command shows that text. By Figure 45.6 the redirection is correct. Standard output goes to the screen, and standard error is captured by the backquotes.
Figure 45.3: File Descriptors Before Redirection
Figure 45.4: File Descriptors After 3>&2 Redirection
Figure 45.5: File Descriptors After 3>&2 2>&1 Redirection
Figure 45.6: File Descriptors After 3>&2 2>&1 1>&3 Redirection
Open files are automatically closed when a process exits.
But it's safer to close the files yourself as soon as you're done with them.
That way, if you forget and use the same descriptor later for something
else (for instance, use F.D. 3 to redirect some other command, or a
subprocess uses F.D. 3), you won't run into conflicts.
Use - |
|