|< Day Day Up >|
1.7. Input and Output
The software fieldreally, any scientific fieldtends to advance most quickly and impressively on those few occasions when someone (i.e., not a committee) comes up with an idea that is small in concept yet
The UNIX I/O scheme is based on two dazzlingly simple ideas. First, UNIX file I/O takes the form of arbitrarily long sequences of
1.7.1. Standard I/O
By convention, each UNIX program has a single way of accepting input called
, a single way of producing output called
, and a single way of producing error messages called
standard error output
Standard I/O was the first scheme of its kind that was designed
When necessary, you can redirect input and output to come from or go to a file instead. If you want to send the contents of a pre-existing file to someone as mail, you redirect mail 's standard input so that it reads from that file instead of your keyboard.
You can also hook programs together in a pipeline , in which the standard output of one program feeds directly into the standard input of another; for example, you could feed mail output directly to the lp program so that messages are printed instead of shown on the screen.
This makes it possible to use UNIX utilities as building blocks for bigger programs. Many UNIX utility programs are
Table 1-5. Popular UNIX data filtering utilities
You may have used some of these before and noticed that they take
For example, the most basic utility is cat , which simply copies its input to its output. If you type cat with a filename argument, it will print out the contents of that file on your screen. But if you invoke it with no arguments, it will expect standard input and copy it to standard output. Try it: cat will wait for you to type a line of text; when you type RETURN, cat will repeat the text back to you. To stop the process, hit CTRL-D at the beginning of a line. You will see ^D when you type CTRL-D. Here's what this should look like:
$ cat Here is a line of text. Here is a line of text. This is another line of text. This is another line of text. ^D $
1.7.2. I/O Redirection
is short for "catenate," i.e., link together. It accepts multiple filename arguments and copies them to the standard output. But let's
For example, if you have a file called cheshire that contains some text, then cat < cheshire will print cheshire 's contents out onto your terminal. sort < cheshire will sort the lines in the cheshire file and print the result on your terminal (remember: we're pretending that these utilities don't take filename arguments).
Similarly, command > filename causes the command 's standard output to be redirected to the named file. The classic "canonical" example of this is date > now : the date command prints the current date and time on the standard output; the previous command saves it in a file called now .
Input and output redirectors can be combined. For example: the cp command is normally used to copy files; if for some reason it didn't exist or was broken, you could use cat in this way:
$ cat < file1 > file2
This would be similar to cp file1 file2 .
It is also possible to redirect the output of a command into the standard input of another command instead of a file. The construct that does this is called the pipe, notated as . A command line that includes two or more commands connected with pipes is called a pipeline.
Pipes are very often used with the
command, which works just like
except that it prints its output screen by screen, pausing for the user to type SPACE (next screen), RETURN (
Pipelines can get very complex, and they can also be combined with other I/O directors. To see a sorted listing of the file cheshire a screen at a time, type sort < cheshire more . To print it instead of viewing it on your terminal, type sort < cheshire lp .
Here's a more complicated example. The file
stores information about users' accounts on a UNIX system. Each line in the file contains a user's login name, user ID number, encrypted password, home directory, login shell, and other information. The first field of each line is the login
To get a sorted listing of all users on the system, type:
$ cut -d: -f1 < /etc/passwd sort
(Actually, you can omit the
accepts input filename arguments.) The
adm bin cam daemon davidqc ftp games gonzo ...
If you want to send the list directly to the printer (instead of your screen), you can extend the pipeline like this:
$ cut -d: -f1 < /etc/passwd sort lp
Now you should see how I/O directors and pipelines support the UNIX building block philosophy. The notation is extremely terse and powerful. Just as important, the pipe concept eliminates the need for messy temporary files to store command output before it is fed into other commands.
For example, to do the same sort of thing as the above command line on other operating systems (
$ cut [etc]passwd /d=":" /f=1 /out=temp1 $ sort temp1 /out=temp2 $ print temp2 $ delete temp1 temp2
After sufficient practice, you will find yourself routinely typing in powerful command pipelines that do in one line what it would take several commands (and temporary files) in other operating systems to accomplish.
|< Day Day Up >|