Datasheet

36
Phase 1
Working on the Command Line
operator (>), which you place after the command and before the name of a file that is to receive
the output:
$ netstat -p > net-connections.txt
The file net-connections.txt now contains the output of the netstat -p command,
with one exception (described shortly). You can then open net-connections.txt in a text
editor or view it with a program such as less in order to study its contents in greater detail.
Do so now and search for references to the Firefox browser. (These may be called firefox-
bin.) Don’t worry about what these references mean, though; the point is to familiarize your-
self with redirection, not the output of netstat.
When you ran the netstat -p command with redirection as an ordinary user, chances are
you saw a message appear on the screen to the effect that not all processes would be displayed.
This message was directed to standard error. It, too, can be redirected, but you must use the
standard error redirection (2>) operator rather than the standard output redirection operator:
$ netstat -p 2> error-messages.txt
If you examine the error-messages.txt file, you’ll see that it contains only the warning
about not all processes being displayed; standard output appears on the screen. You can redi-
rect both standard output and standard error by using the &> redirection operator:
$ netstat -p &> net-connections.txt
All of these redirection operators overwrite whatever file you provide as an argument (net-
connections.txt or error-messages.txt in these examples). When redirecting standard
output or standard error (but not both), you can append to an existing file rather than over-
write it by adding a second greater-than symbol:
$ netstat -p >> net-connections.txt
$ netstat -p 2>> error-messages.txt
A common trick is to redirect standard output or standard error to /dev/null.
This file is a device that’s connected to nothing; it’s used when you want to
get rid of data. For instance, if the whine program is generating error mes-
sages you don’t care about, you might type whine 2> /dev/null to run it and
discard its error messages.
Redirecting Input
Many Linux programs are designed to accept input from files whose names you specify on the
command line. Some programs, though, are designed to accept keyboard input via standard
input. What if you want to provide fixed input from a file to such programs, though? The
answer is to use standard input redirection via the input redirection operator (<).
As an example, suppose you wanted to convert the error-messages.txt file to a graphics
format. You might want to use the text2gif program (part of the giflib package), but it
83484.book Page 36 Monday, September 18, 2006 8:58 AM