Datasheet
Task 1.7: Use Streams, Pipes, and Redirection
35
This task may be performed as an ordinary user. If you run it as root, you run the usual
risks of performing tasks as root.
Procedure
This task investigates three practical techniques: redirecting output, redirecting input, and pip-
ing data between programs. All three techniques rely on the fact that Linux uses input and out-
put streams, so you should first understand a bit of theory.
Understanding Streams
To Linux programs, input and output involve files. A program can read or write data from or
to a disk file, but even the keyboard and screen are treated much like files. These devices cor-
respond to three separate streams:
Standard input This stream, often abbreviated stdin, corresponds to the keyboard. When a
program wants input from the user, it opens standard input and reads data from it as if it were
reading data from a disk file.
Standard output This stream is often referred to as stdout, and it corresponds to the text-
mode display, which can mean a text-mode console, the contents of an xterm or similar win-
dow, or a remote login session’s display. Programs write data to standard output as if it were
a file, and the characters so written then appear on the screen.
Standard error Ordinarily, this stream (often abbreviated stderr) is the same physical device
as standard output; however, it’s used by programs to display error messages rather than ordi-
nary output. The reason it’s treated separately is so that it may be redirected separately—so
that you can interact normally with a program while sending error message to a file or so that
you can redirect normal output to a file while still seeing error messages on the screen.
If you use a Linux computer at the console (that is, using the keyboard and monitor that
are attached directly to the computer), these streams correspond to the computer’s own key-
board and display. Linux systems can also be used remotely, via logins using protocols such
as the Secure Shell (SSH). In such cases, standard input, standard output, and standard error
are all directed over the remote login protocol and so ultimately correspond to a keyboard and
monitor on the remote computer.
Redirecting Output
To begin the task, you want to investigate the network connections maintained by Firefox. To
do so, you’ll use the netstat command, which displays information on all the network con-
nections maintained by the computer. You’ll use the -p option to netstat, so as to display the
program names:
$ netstat -p
When you type this command, though, chances are you’ll see so much output scroll past
that you won’t be able to read it all before most of it disappears off the top of the screen. One
way around this problem is to redirect standard output to a file. You do this with the redirection
83484.book Page 35 Monday, September 18, 2006 8:58 AM










