Previous Section  < Day Day Up >  Next Section

7.10. Standard I/O and Redirection

When the shell starts up, it inherits three files: stdin, stdout, and stderr. Standard input normally comes from the keyboard. Standard output and standard error normally go to the screen. There are times when you want to read input from a file or send output or error to a file. This can be accomplished by using I/O redirection. See Table 7.9 for a list of redirection operators.

Example 7.37.

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 otuput

    $ 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 > foundit 2> /dev/null     # Redirect output to foundit,

                                                          # and error to /dev/null

6   $ find . –name \*.c –print > foundit 2>&1        # Redirect output and send standard

                                                     # error to where output is going

7   $ echo "File needs an argument" 1>&2             # Send standard output to error


Table 7.9. Redirection Operators

Operator

What It Does

<

Redirect input

>

Redirect output

>>

Append output

2>

Redirect error

1>&2

Redirect output to where error is going

2>&1

Redirect error to where output is going


EXPLANATION

  1. Instead of getting input from the keyboard, standard input is redirected from the file myfile to the UNIX tr command. All uppercase letters are converted to lowercase letters.

  2. Instead of sending output to the screen, 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, the standard error is redirected to the file errfile. Now you can take your error file to the local guru for an explanation (of sorts)!

  5. The find command starts searching in the current working directory for filenames ending in .c, and prints the filenames to a file named foundit. 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 filenames to a file named foundit. The errors are also sent to foundit.

  7. The echo command sends its message to standard error. Its standard output is merged with standard error.

7.10.1 The exec Command and Redirection

The exec command can be used to replace the current program with a new one without starting a new process. Standard output or input can be changed with the exec command without creating a subshell (see Table 7.10). If a file is opened with exec, subsequent read commands will move the file pointer down the file a line at a time until the end of the 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. For examples on how to use exec commands in scripts, see "Looping Control Commands" on page 358.

Example 7.38.

1   $ exec date

    Sun Oct 17 10:07:34  PDT 2004

     <Login prompt appears if you are in your login shell >

2   $ exec > temp

    $ ls

    $ pwd

    $ echo Hello

3   $ exec > /dev/tty 

4   $ echo Hello

    Hello


Table 7.10. The exec Command

Command

What It Does

exec ls

Execute ls 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 3< datfile

Open datfile as file descriptor 3 for reading input.

sort <&3

Sort datfile.

exec 4>newfile

Open newfile as file descriptor (fd) 4 for writing.

ls >&4

Output of ls is redirected to newfile.

exec 5<&4

Make fd 5 a copy of fd 4.

exec 3<&–

Close fd 3.


EXPLANATION

  1. The exec command executes the date command in the current shell (it does not fork a child shell). Because the date command is executed in place of the current shell, when the date command exits, the shell terminates. If a Bourne shell had been started from the C shell, the Bourne shell would exit and the C shell prompt would appear. If you are in your login shell when you try this, you will be logged out. If you are working interactively in a shell window, the window exits.

  2. The exec command opens standard output for the current shell to the temp file. Output from ls, pwd, and echo will no longer go to the screen, but to temp.

  3. The exec command reopens standard output to the terminal. Now, output will go to the screen as shown in line 4.

  4. Standard output has been directed back to the terminal (/dev/tty).

Example 7.39.

1   $ cat doit

    pwd

    echo hello

    date

2   $ exec < doit

    /home/jody/ellie/shell

    hello

    Sun Oct 17 10:07:34  PDT 2004

3   %


EXPLANATION

  1. The contents of a file called doit are displayed.

  2. The exec command opens standard input to the file called doit. Input is read from the file instead of from the keyboard. The commands from the file doit are executed in place of the current shell. When the last command exits, so does the shell. See Figure 7.2.

  3. The Bourne shell exited when the exec command completed. The C shell prompt appeared. It was the parent shell. If you had been in your login shell when the exec finished, you would be logged out; if in a window, the window would have disappeared.

Figure 7.2. The exec command.


Example 7.40.

1   $ exec 3> filex

2   $ who >& 3

3   $ date >& 3

4   $ exec 3>&-

5   $ exec 3< filex

6   $ cat <&3

    ellie   console   Oct 7    09:53

    ellie   ttyp0     Oct 7    09:54

    ellie   ttyp1     Oct 7    09:54

    ellie   ttyp2     Oct 11   15:42

    Sun Oct 17 13:31:31  PDT 2004

7   $ exec 3<&-

8   $ date >& 3

    Sun Oct 17 13:41:14  PDT 2004


EXPLANATION

  1. File descriptor 3 (fd 3) is assigned to filex and opened for redirection of output. See Figure 7.3.

    Figure 7.3. exec and file descriptors.


  2. The output of the who command is sent to fd 3, filex.

  3. The output of the date command is sent to fd 3; filex is already opened, so the output is appended to filex.

  4. Fd 3 is closed.

  5. The exec command opens fd 3 for reading input. Input will be redirected from filex.

  6. The cat program reads from fd 3, assigned to filex.

  7. The exec command closes fd 3. (Actually, the operating system will close the file once end of file is reached.)

  8. When attempting to send the output of the date command to fd 3, the output goes to the screen, because the file descriptor was closed.

    Previous Section  < Day Day Up >  Next Section