Section 5.6. Redirection


[Page 172 (continued)]

5.6. Redirection

The shell redirection facility allows you to:

  • store the output of a process to a file (output redirection)

  • use the contents of a file as input to a process (input redirection)

Let's have a look at each facility, in turn.


[Page 173]

5.6.1. Output Redirection

Output redirection is handy because it allows you to save a process's output into a file so it can be listed, printed, edited, or used as input to a future process. To redirect output, use either the > or >> metacharacters. The sequence

$ command > fileName 


sends the standard output of command to the file with name fileName. The shell creates the file with name fileName if it doesn't already exist; otherwise, it overwrites its previous contents. If the file already exists and doesn't have write permission, an error occurs. In the following example, I created a file called "alice.txt" by redirecting the output of the cat utility. Without parameters, cat simply copies its standard inputwhich in the case is this keyboardto its standard output.

$ cat > alice.txt                 ...create a text file. In my dreams that fill the night, I see your eyes, ^D                                ...end-of-input. $ cat alice.txt                   ...look at its contents. In my dreams that fill the night, I see your eyes, $ _ 


The sequence

$ command >> fileName 


appends the standard output of command to the file with name fileName. The shell creates the file with name fileName if it doesn't already exist. In the following example, I appended some text to the existing "alice.txt" file:

$ cat >> alice.txt           ...append to the file. And I fall into them, Like Alice fell into Wonderland. ^D                           ...end-of-input. $ cat alice.txt              ...look at the new contents. In my dreams that fill the night, I see your eyes, And I fall into them, Like Alice fell into Wonderland. $ _ 


By default, both forms of output redirection leave the standard error channel connected to the terminal. However, both shells have variations of output redirection that allow them to redirect the standard error channel. The C, Korn, and Bash shells also provide protection against accidental overwriting of a file due to output redirection. These facilities are described in later chapters.


[Page 174]

5.6.2. Input Redirection

Input redirection is useful because it allows you to prepare a process's input and store it in a file for later use. To redirect input, use either the < or << metacharacters. The sequence:

$ command < fileName 


executes command using the contents of the file fileName as its standard input. If the file doesn't exist or doesn't have read permission, an error occurs. In the following example, I sent myself the contents of "alice.txt" via the mail utility:

$ mail glass < alice.txt              ...send myself mail. $ mail                                ...look at my mail. Mail version 8.1 6/6/93.  Type ? for help. >N  1 glass@utdallas.edu Mon Feb  2 13:29   17/550 & 1                                 ...read message #1. From: Graham Glass <glass@utdallas.edu> To: glass@utdallas.edu In my dreams that fill the night, I see your eyes, And I fall into them, Like Alice fell into Wonderland & q                                   ...quit mail. $ _ 


When the shell encounters a sequence of the form:

$ command << word 


it copies its standard input up to but not including the line starting with word into a buffer and then executes command using the contents of the buffer as its standard input. This facility is used almost exclusively to allow shell programs (scripts) to supply the standard input to other commands as inline text, and is revisited in more detail later on in this chapter.




Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net