11.13. Standard I/O and Redirection
The Korn shell opens three files (called streams) whenever a program is started: stdin, stdout, and stderr. Standard input normally comes from the keyboard and is associated with file descriptor 0. Standard output normally goes to the screen, file descriptor 1. Standard error normally goes to the screen, file descriptor 2. Standard input, output, and error can be redirected to or from a file. See Table 11.18 for a list of redirection operators.
Table 11.18. RedirectionOperator | Function |
---|
< file | Redirect input from file | > file | Redirect output to file | >> file | Redirect and append output to file | 2> file | Redirect errors to file | 2>> file | Redirect and append errors to file | 1>&2 | Redirect output to where error is going | 2>&1 | Redirect error to where output is going |
Example 11.63.
(The Command Line)
1 $ tr '[A-Z]' '[a-z]' < myfile # Redirect input
2 $ ls > lsfile # Redirect output
$ cat lsfile
dir1
dir2
file1
file2
file3
3 $ date >> lsfile # Redirect and append output
$ cat lsfile
dir1
dir2
file1
file2
file3
Mon Sept 20 12:57:22 PDT 2004
4 $ cc prog.c 2> errfile # Redirect error
5 $ find . –name \*.c –print > founditfile 2> /dev/null
6 $ find . –name \*.c –print > foundit 2>&1
7 $ print "File needs an argument" 1>&2
8 $ function usage { print "Usage: $0 [-y] [-g] filename" 1>&2 ; exit 1; }
EXPLANATION
The standard input is redirected from the file myfile to the UNIX tr command. All uppercase letters are converted to lowercase letters. The ls command redirects its output to the file lsfile. The output of the date command is redirected and appended to lsfile. The file prog.c is compiled. If the compile fails, standard error is redirected to errfile. The find command starts searching in the current working directory for filenames ending in .c and prints the files to a file named founditfile. Errors from the find command are sent to /dev/null. The find command starts searching in the current working directory for filenames ending in .c and prints the files to a filenamed foundit. The standard error (file descriptor 2) is being sent to the same place that the standard output (file descriptor 1) is being sent, to the file called foundit. The print command sends its message to standard error. Standard output is merged with standard error; that is, standard output is being redirected to the place where standard error goes, the terminal. This makes it possible to separate error messages from "good" output. The function usage is defined. This function, when called, will print a usage message, send the output to standard error, and exit. This type of function is often used in scripts.
11.13.1 The exec Command and Redirection
The exec command can be used to replace the current program with the one being execed. Another use for the exec command is to change standard output or input without creating a subshell. If a file is opened with exec, subsequent read commands will move the file pointer down the file a line at a time until end of file. The file must be closed to start reading from the beginning again. However, if using UNIX utilities such as cat and sort, the operating system closes the file after each command has completed. See Table 11.19 for exec functionality.
Table 11.19. exec CommandsCommand | Function |
---|
exec ls | ls will execute in place of the shell. When ls is finished, the shell in which it was started does not return. | exec < filea | Open filea for reading standard input. | exec > filex | Open filex for writing standard output. | exec 2> errors | Open errors for writing standard error. | exec 2>> errors | Open errors for writing and appending standard error. | exec 2> /dev/console | Sends all error messages to the console. | exec 3< datfile | Open datfile as file descriptor 3 for reading input. | sort <&3 | datfile is sorted. | exec 4>newfile | Open newfile as file descriptor 4 for writing. | ls >&4 | Output of ls is redirected to newfile. | exec 5<&4 | Make fd 5 a copy of fd 4. Both descriptors refer to newfile. | exec 3<&– | Close file descriptor 3, datfile. |
11.13.2 Redirection and the Child Shell
When the output of a command is redirected from the screen to a file, the Korn shell creates (forks) a child shell to rearrange the file descriptors, as shown in Figure 11.2.
|