IO Redirection

By default, standard output goes to the display (generally a terminal emulator window), but that's just the default. You can change the endpoint for a data stream coming from standard output. The data from standard output can be sent to a file on disk instead. A file is a file; data traffic between files is handled the same way by Linux, so switching endpoints is no big trick. Data from standard output can be sent to an existing file or it can be sent to a new file created when your program is run.

By default, input to your programs comes from the keyboard, but all the keyboard sends is text. This text could also come from another text file. Switching the source of data sent to your programs is no more difficult than switching the destination of its output. The mechanism for doing so is called I/O redirection, and we're going to use it for a lot of the example programs in this book.

You've probably already used I/O redirection in your Linux work, even if you didn't know it by name. All of Linux's basic shell commands send their output to standard output. The ls command, for example, sends a listing of the contents of the working directory to standard output. You can capture that listing by redirecting the text emitted by ls into a Linux disk file. To do so, enter this command at the command line:

ls > dircontents.txt

The file dircontents.txt is created if it doesn't already exist, and the text emitted by ls is stored in dircontents.txt. You can then print the file or load it into a text editor.

The ">" symbol is one of two redirection operators. The "<" symbol works the other way, and redirects standard input away from the keyboard and to another file, typically a text file stored on disk. This is less useful for handing keyboard commands to a program than it is for providing the raw material on which the program is going to work.

Let's say you want to write a program to force all the lowercase text in a file to uppercase characters. (This is a wonderfully contrarian thing to do, as uppercase characters make some Unix people half-nuts.) You can write the program to obtain its text from standard input and send its text to standard output. This is very easy to do from a programming standpoint—and in fact we'll be doing it a little further along in the book.

You can test your program by typing a line of text at the keyboard:

i want live things in their pride to remain.

Your program would process the preceding line of text and send the processed text to standard output, where it would be posted to the terminal emulator display:

I WANT LIVE THINGS IN THEIR PRIDE TO REMAIN.

Well, the test was a success: it looks like things worked inside the program. The next step is to test uppercaser on some real files. You don't have to change the uppercaser program at all. Just enter this at the shell prompt:

uppercaser < santafetrail.txt > vachelshouting.txt

By the magic of I/O redirection, your program will read all the text from a disk file called santafetrail.txt, force any lowercase characters to uppercase, and then write the uppercase text to the disk file vachelshouting.txt.

The redirection operators can be thought of as arrows pointing in the direction that data is moving. Data is being taken from the file santafetrail.txt and sent to the uppercaser program; thus the symbol < points from the input file to the program where it's going. The uppercaser program is sending data to the output file vachelshouting.txt, and thus the redirection operator points away from the name of the program and toward the name of the output file.

From a height, what's going on looks like what I've drawn in Figure 6-10. I/O redirection acts as a sort of data switch, steering streams of data away from the standard files to named source and destination files of your own choosing.

Display

Display

1 /-tf-tf-vv /-vy-vs /-vvv-^ rw~\ li

n

n

l,Ln

r^i

Keyboard

K

< File

Figure 6-10: I/O redirection

Was this article helpful?

0 0

Post a comment