11.13. Standard IO and Redirection

 <  Day Day Up  >  

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. Redirection

Operator

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: 
 (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; }  
[-y] [-g] filename" 1>&2 ; exit 1; }

EXPLANATION

  1. The standard input is redirected from the file myfile to the UNIX tr command. All uppercase letters are converted to lowercase letters .

  2. The ls command redirects its output to the file lsfile .

  3. The output of the date command is redirected and appended to lsfile .

  4. The file prog.c is compiled. If the compile fails, standard error is redirected to errfile .

  5. 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 .

  6. 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 .

  7. 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.

  8. 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 exec ed. 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 Commands

Command

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.

Figure 11.2. Redirection of standard output and errors.

 <  Day Day Up  >  


UNIX Shells by Example
UNIX Shells by Example (4th Edition)
ISBN: 013147572X
EAN: 2147483647
Year: 2004
Pages: 454
Authors: Ellie Quigley

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