< Day Day Up > |
9.9. Redirection and PipesNormally, standard output (stdout) from a command goes to the screen, standard input (stdin) comes from the keyboard, and error messages (stderr) go to the screen. The shell allows you to use the special redirection metacharacters to redirect the input/output to or from a file. The redirection operators (<, >, >>, >&) are followed by a filename. This file is opened by the shell before the command on the left-hand side is executed. Pipes, represented by a vertical bar (|) symbol, allow the output of one command to be sent to the input of another command. The command on the left-hand side of the pipe is called the writer because it writes to the pipe. The command on the right-hand side of the pipe is the reader because it reads from the pipe. See Table 9.5 for a list of redirection and pipe metacharacters.
9.9.1 Redirecting InputInstead of the input coming from the terminal keyboard, it can be redirected from a file. The shell will open the file on the right-hand side of the < symbol and the program on the left will read from the file. If the file does not exist, the error No such file or directory will be reported by the C shell. FORMAT command < file Example 9.36.
mail bob < memo
EXPLANATION The file memo is opened by the shell, and the input is redirected to the mail program. Simply, the user bob is sent a file called memo by the mail program. 9.9.2 The here documentThe here document is another way to redirect input to a command. It is used in shell scripts for creating menus and processing input from other programs. Normally, programs that accept input from the keyboard are terminated with Ctrl-D (^D). The here document provides an alternate way of sending input to a program and terminating the input without typing ^D. The << symbol is followed by a user-defined word, often called a terminator. Input will be directed to the command on the left-hand side of the << symbol until the user-defined terminator is reached. The final terminator is on a line by itself, and cannot be surrounded by any spaces. Variable and command substitution are performed within the here document. (Normally, a here document is used in shell scripts to create menus and provide input to commands such as mail, bc, ex, ftp, etc.) FORMAT command << MARK ... input ... MARK Example 9.37.(Without the here document) (The Command Line) 1 % cat 2 Hello There. How are you? I'm tired of this. 3 ^D (The Output) 4 Hello There. How are you? I'm tired of this. EXPLANATION
Example 9.38.(With the here document) (The Command Line) 1 % cat << DONE 2 Hello There. How are you? I'm tired of this. 3 DONE (The Output) 4 Hello There. How are you? I'm tired of this. EXPLANATION
Example 9.39.(The Command Line) 1 % set name = steve 2 % mail $name << EOF 3 Hello there, $name 4 The hour is now `date +%H` 5 EOF EXPLANATION
9.9.3 Redirecting OutputBy default, the standard output of a command or commands goes to the terminal screen. To redirect standard output from the screen to a file, the > symbol is used. The command is on the left-hand side of the > symbol, and a filename is on the right-hand side. The shell will open the file on the right-hand side of the > symbol. If the file does not exist, the shell will create it; if it does exist, the shell will open the file and truncate it. Often files are inadvertently removed when using redirection. (A special C/TC shell variable, called noclobber, can be set to prevent redirection from clobbering an existing file. See Table 9.6 on page 439.)
FORMAT command > file Example 9.40.
cat file1 file2 > file3
EXPLANATION The contents of file1 and file2 are concatenated and the output is sent to file3. Remember that the shell opens file3 before it attempts to execute the cat command. If file3 already exists and contains data, the data will be lost. If file3 does not exist, it will be created. 9.9.4 Appending Output to an Existing FileTo append output to an existing file, the >> symbol is used. If the file on the right-hand side of the >> symbol does not exist, it is created; if it does exist, the file is opened and output is appended to the end of the file. FORMAT command >> file Example 9.41.
date >> outfile
EXPLANATION The standard output of the date command is redirected and appended to outfile. 9.9.5 Redirecting Output and ErrorThe >& symbol is used to redirect both standard output and standard error to a file. Normally, a command is either successful and sends its output to stdout, or fails and sends its error messages to stderr. Some recursive programs, such as find and du, send both standard output and errors to the screen as they move through the directory tree. By using the >& symbol, both standard output and standard error can be saved in a file and examined. The C/TC shell does not provide a symbol for redirection of only standard error, but it is possible to get just the standard error by executing the command in a subshell. See Example 9.42 and Figure 9.2. Example 9.42.1 % date Tue Aug 9 10:31:56 PDT 2004 2 % date >& outfile 3 % cat outfile Tue Aug 9 10:31:56 PDT 2004 Figure 9.2. Redirecting stdout and stderr. See Example 9.42.EXPLANATION
Example 9.43.1 % cp file1 file2 2 % cp file1 Usage: cp [-ip] f1 f2; or: cp [-ipr] f1 ... fn d2 3 % cp file1 >& errorfile 4 % cat errorfile Usage: cp [-ip] f1 f2; or: cp [-ipr] f1 ... fn d2 EXPLANATION
9.9.6 Separating Output and ErrorsStandard output and standard error can be separated by enclosing the command in parentheses. When a command is enclosed in parentheses, the C/TC shell starts up a subshell, handles redirection from within the subshell, and then executes the command. By using the technique shown in Example 9.44, the standard output can be separated from the errors. Example 9.44.(The Command Line) 1 % find . -name '*.c' -print >& outputfile 2 % (find . -name '*.c' -print > goodstuff) >& badstuff EXPLANATION
9.9.7 The noclobber VariableThe special shell built-in variable noclobber, when set, protects you from clobbering files with redirection. See Table 9.6. Example 9.45.1 % cat filex abc 123 2 % date > filex 3 % cat filex Wed Aug 5 11:51:04 PDT 2004 4 % set noclobber 5 % date > filex filex: File exists. 6 % ls >! filex # Override noclobber for this command only % cat filex abc ab1 dir filex plan.c 7 % ls > filex filex: File exists. 8 % unset noclobber # Turn off noclobber permanently EXPLANATION
|
< Day Day Up > |