ch05.xml.pdf

(77 KB) Pobierz
In this chapter:
• Standard Input and
Standard Output
• Pipes and Filters
5
Redirecting I/O
Many Unix programs read input (such as a file) and write output. In this
chapter, we discuss Unix programs that handle their input and output in a
standard way. This lets them work with each other.
This chapter generally
doesn’t
apply to full-screen programs, such as the
Pico editor, that take control of your whole terminal window. (The pager
programs,
less, more,
and
pg,
do
work together in this way.) It also
doesn’t apply to graphical programs, such as StarOffice or Netscape, that
open their own windows on your screen.
Standard Input and Standard
Output
What happens if you don’t give a filename argument on a command line?
Most programs will take their input from your keyboard instead (after you
press the first RETURN to start the program running, that is). Your termi-
nal keyboard is the program’s
standard input.
As a program runs, the results are usually displayed on your terminal
screen. The terminal screen is the program’s
standard output.
So, by default, each of these programs takes its input from the standard
input and sends the results to the standard output.
These two default cases of input/output (I/O) can be varied. This is called
I/O redir ection.
If a program doesn’t normally read from files, but reads from its standard
input, you can give a filename by using the < (less-than symbol) operator.
87
7 January 2002 13:13
88
Chapter 5: Redirecting I/O
For example, the
mail
program (see the section “Sending Mail from a Shell
Prompt” in Chapter 6) normally reads the message to send from your key-
board. Here’s how to use the input redirection operator to mail the con-
tents of the file
to_do
to
bigboss@corp.xyz:
$
mail bigboss@corp.xyz < to_do
$
If a program writes to its standard output, which is normally the screen,
you can make it write to a file instead by using the greater-than symbol
(
>
) operator. The pipe operator (
|
) sends the standard output of one pro-
gram to the standard input of another program. Input/output redirection is
one of the most powerful and flexible Unix features, We’ll take a closer
look at it soon.
Putting Text in a File
Instead of always letting a program’s output come to the screen, you can
redirect output into a file. This is useful when you’d like to save program
output or when you put files together to make a bigger file.
cat
cat,
which is short for “concatenate,” reads files and outputs their contents
one after another, without stopping.
To display files on the standard output (your screen), use:
cat
file(s)
For example, let’s display the contents of the file
/etc/passwd.
This system
file describes users’ accounts. (Your system may have a more complete list
somewhere else.)
$
cat /etc/passwd
root:x&k8KP30f;(:0:0:Root:/:
daemon:*:1:1:Admin:/:
.
.
.
john::128:50:John Doe:/usr/john:/bin/sh
$
You cannot go back to view the previous screens, as you can when you
use a pager program such as
less
(unless you’re using a terminal window
with a scrollbar, that is).
cat
is mainly used with redirection, as we’ll see in
a moment.
7 January 2002 13:13
Standard Input and Standard Output
89
By the way: if you enter
cat
without a filename, it tries to read from the
keyboard (as we mention earlier). You can get out by pressing
RETURN followed by a single CTRL-D .
The > operator
When you add “>
filename”
to the end of a command line, the program’s
output is diverted from the standard output to the named file. The > sym-
bol is called the
output redir ection operator.
When you use the > operator, be careful not to accidentally
overwrite a file’s contents. Your system may let you redirect
output to an existing file. If so, the old file will be deleted
(or, in Unix lingo, “clobbered”). Be careful not to overwrite
a much needed file!
Many shells can protect you from this risk. In the C shell,
use the command
set noclobber.
The Korn shell and
bash
command is
set –o noclobber.
Enter the command at a shell
prompt or put it in your shell’s startup file. After that, the
shell does not allow you to redirect onto an existing file
and overwrite its contents.
This doesn’t protect against overwriting by Unix programs
such as
cp;
it works only with the > redirection operator.
For more protection, you can set Unix file access permis-
sions.
For example, let’s use
cat
with this operator. The file contents that you’d
normally see on the screen (from the standard output) are diverted into
another file, which we’ll then read using
cat
(without any redirection!):
$
cat /etc/passwd > password
$
cat password
root:x&k8KP30f;(:0:0:Root:/:
daemon:*:1:1:Admin:/:
.
.
.
john::128:50:John Doe:/usr/john:/bin/sh
$
An earlier example (in the section “cat”) showed how
cat /etc/passwd
dis-
plays the file
/etc/passwd
on the screen. The example here adds the >
7 January 2002 13:13
90
Chapter 5: Redirecting I/O
operator; so the output of
cat
goes to a file called
password
in the work-
ing directory. Displaying the file
password
shows that its contents are the
same as the file
/etc/passwd
(the effect is the same as the copy command
cp /etc/passwd password).
You can use the > redirection operator with any program that sends text
to its standard output—not just with
cat.
For example:
$
who > users
$
date > today
$
ls
password
today
users
...
We’ve sent the output of
who
to a file called
users
and the output of
date
to the file named
today.
Listing the directory shows the two new files.
Let’s look at the output from the
who
and
date
programs by reading these
two files with
cat:
$
cat users
tim
tty1
Aug 12 07:30
john
tty4
Aug 12 08:26
$
cat today
Tue Aug 12 08:36:09 EDT 2001
$
You can also use the
cat
program and the > operator to make a small text
file. We told you earlier to type CTRL-D if you accidentally enter
cat
with-
out a filename. This is because the
cat
program alone takes whatever you
type on the keyboard as input. Thus, the command:
cat >
filename
takes input from the keyboard and redirects it to a file. Try the following
example:
$
cat > to_do
Finish report by noon
Lunch with Xannie
Swim at 5:30
ˆD
$
cat
takes the text that you typed as input (in this example, the three lines
that begin with
Finish
,
Lunch
, and
Swim
), and the > operator redirects it to
a file called
to_do.
Type CTRL-D
once,
on a new line by itself, to signal
the end of the text. You should get a shell prompt.
7 January 2002 13:13
Standard Input and Standard Output
91
You can also create a bigger file from smaller files with the
cat
command
and the > operator. The form:
cat
file1 file2 > newfile
creates a file
newfile,
consisting of
file1
followed by
file2.
$
cat today to_do > diary
$
cat diary
Tue Aug 12 08:36:09 EDT 2001
Finish report by noon
Lunch with Xannie
Swim at 5:30
$
You can’t use redirection to add a file to itself, along with
other files. For example, you might hope that the following
command would merge today’s to-do list with tomorrow’s.
This won’t work!
$
cat to_do to_do.tomorrow > to_do.tomorrow
cat: to_do.tomorrow: input file is output file
cat
warns you, but it’s actually already too late. When you
redirect a program’s output to a file, Unix empties (clob-
bers) the file
befor e
the program starts running. The right
way to do this is by using a temporary file (as you’ll see in
a later example) or simply by using a text editor program.
The >> operator
You can add more text to the end of an existing file, instead of replacing
its contents, by using the >> (append redirection) operator. Use it as you
would the > (output redirection) operator. So:
cat
file2
>>
file1
appends the contents of
file2
to the end of
file1.
For an example, let’s
append the contents of the file
users,
and also the current date and time,
to the file
diary.
Then we display the file:
$
cat users >> diary
$
date >> diary
$
cat diary
Tue Aug 12 08:36:09 EDT 2001
7 January 2002 13:13
Zgłoś jeśli naruszono regulamin