Previous Section  < Day Day Up >  Next Section

13.17. 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 errors to a file. This can be accomplished by using I/O redirection. See Table 13.23 for a list of redirection operators.

Example 13.83.

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

    Sun Sept 17 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 errors to /dev/null,

    # respectively.



6   $ find . –name \*.c –print >& foundit

    # Redirect both output and errors to foundit.



7   $ find . –name \*.c –print > foundit 2>&1

    # Redirect output to foundit and send errors to where output

    # is going; i.e. foundit



8   $ echo "File needs an argument" 1>&2

    # Send standard output to error


Table 13.23. Redirection

Redirection Operator

What It Does

< filename

Redirects input

> filename

Redirects output

>> filename

Appends output

2> filename

Redirects error

2>> filename

Redirects and appends error

&> filename

Redirects output and error

>& filename

Redirects output and error (preferred way)

2>&1

Redirects error to where output is going

1>&2

Redirects output to where error is going

>|

Overrides noclobber when redirecting output

<> filename

Uses file as both standard input and output if a device file (from /dev)


EXPLANATION

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

  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 C program source 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. Same as 6.

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

13.17.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 13.24.) 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.

Example 13.84.

1    $ exec date

     Thu Oct 14 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 13.24. The exec Command

Command

What It Does

exec ls

ls executes in place of the shell. When ls is finished, the shell in which it was started does not return.

exec < filea

Opens filea for reading standard input.

exec > filex

Opens filex for writing standard output.

exec 3< datfile

Opens datfile as file descriptor 3 for reading input.

sort <&3

Datfile is sorted.

exec 4>newfile

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

ls >&4

Output of ls is redirected to newfile.

exec 5<&4

Makes fd 5 a copy of fd 4.

exec 3<&–

Closes fd 3.


EXPLANATION

  1. The exec command executes the date command in the current shell (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 bash shell had been started from the TC shell, the bash shell would exit and the TC 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. (See Figure 13.2.)

    Figure 13.2. The exec command and file descriptors.


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

1    > bash

2    $ cat doit

     pwd

     echo hello

     date

3    $ exec < doit

     /home/homebound/ellie/shell

     hello

     Thu Oct 14 10:07:34  PDT 2004

4    >


EXPLANATION

  1. From a TC shell prompt, bash is started up. (This is done so that when the exec command exits, the user will not be logged out.)

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

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

  4. The bash shell exited when the exec command completed. The TC 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.

Example 13.86.

1    $ exec 3> filex

2    $ who >& 3

3    $ date >& 3

4    $ exec 3>&-

5    $ exec 3<filex

6    $ cat <&3

     ellie    tty1   Jul 21 09:50

     ellie    ttyp1  Jul 21 11:16  (:0.0)

     ellie    ttyp0  Jul 21 16:49  (:0.0)

     Wed Jul 21 17:15:18 PDT 2004

7    $ exec 3<&-

8    $ date >& 3

     date: write error: Bad file descriptor


EXPLANATION

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

    Figure 13.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. See Figure 13.3(b).

  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, bash reports an error condition, because fd 3 was previously closed.

    Previous Section  < Day Day Up >  Next Section