Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Appending to a File

Overwriting a file simply by redirecting output to it is often undesirable. Fortunately, the shell provides a second form of output redirection with the >> operator, which appends output to a file. The basic syntax is

command >> file
list >> file

In these forms, output is appended to the end of the specified file, or the specified file is created if it does not exist. For example, you can prevent the loss of data from the file mylog each time a date is added, by using the following command:

{ date; uptime; who ; } >> mylog

If you view the contents of mylog, now you find that it contains the output of both lists:

11:15am  up 79 days, 14:48,  5 users,  load average: 0.00, 0.00, 0.00
ranga    tty1     Aug 26 14:12
ranga    ttyp2    Aug 26 14:13 (:0.0)
ranga    ttyp0    Oct 27 19:42 (:0.0)
amma     ttyp3    Oct 30 08:20 (localhost)
ranga    ttyp4    Nov 14 11:13 (rishi.bosland.u)
Sat Nov 14 11:15:54 PST 1998
 11:16am  up 79 days, 14:48,  5 users,  load average: 0.00, 0.00, 0.00
ranga    tty1     Aug 26 14:12
ranga    ttyp2    Aug 26 14:13 (:0.0)
ranga    ttyp0    Oct 27 19:42 (:0.0)
amma     ttyp3    Oct 30 08:20 (localhost)
ranga    ttyp4    Nov 14 11:13 (rishi.bosland.u)

Redirecting Output to a File and the Screen

In certain instances, you need to direct the output of a script to a file and onto the terminal. An example of this is shell scripts that are required to produce a log file of their activities. For interactive scripts, the log file cannot just contain the script’s output redirected to a file.

To redirect output to a file and the screen, use the tee command. The basic syntax is as follows:

command | tee file

Here command is the name of a command, such as ls, and file is the name of the file where you want the output written. For example, the command

$ date | tee now

produces the following output on the terminal:

Sat Nov 14 19:50:16 PST 1998

The same output is written to the file now.

For shell scripts that require all their output to be logged, the following if statement is often used:

if [ "$LOGGING" != "true" ] ; then
    LOGGING="true" ; export LOGGING ;
    exec $0 | tee $LOGFILE
fi

Here you check to see whether a variable, $LOGGING, indicates that logging is turned on. If it is, the script continues; otherwise, the script reruns, and tee sends the output to a log file. To record all the output from a script, this if statement is usually one of the first commands in a script.

Input

Many UNIX programs are interactive and read input from the user. To use such programs in shell scripts, you need to provide them with input in a noninteractive manner. Also, scripts often need to ask the user for input in order to execute commands correctly.

To provide input to interactive programs or to read input from the user, you need to use input redirection. In this section, you will look at the following two methods in detail:

  Input redirection from files
  Reading input from a user
  Redirecting the output of one command to the input of another

Input Redirection

When you need to use an interactive command such as mail in a script, you need to provide the command with input. One method for doing this is to store the input of the command in a file and then tell the command to read input from that file. You accomplish this using input redirection.

The input can be redirected in a manner similar to output redirection. In general, input redirection is

command < file

Here the contents of file become the input for command. For example, the following would be an excellent use of redirection:

Mail ranga@soda.berkeley.edu < Final_Exam_Answers

Here the input to the Mail command, which becomes the body of the mail message, is the file Final_Exam_Answers. In this particular example, a professor might perform this function, and the file might contain the answers to a current final exam.

Here Documents


An additional use of input redirection is in the creation of here documents. A common use of here documents is in the generation of email messages within scripts and in the generation of files containing the values of all the variables in the script. Also, here documents store temporary information. Say you need to send a list of phone numbers or URLs to the printer. By using a here document, you can enter the information that you want to send to the printer into the here document and then send that here document to the printer. This is much simpler than using a temporary file, which needs to be created and then deleted.

The general form for a here document is

command << delimiter
document
delimiter


Here the shell interprets the << operator as an instruction to read input until it finds a line containing the specified delimiter. All the input lines up to the line containing the delimiter are then fed into the standard input of the command.

The delimiter tells the shell that the here document has completed. Without it, the shell continues to read input forever. The delimiter must be a single word that does not contain spaces or tabs.


For example, to print a quick list of URLs, you could use the following here document:

lpr << MYURLS
     http://www.csua.berkeley.edu/∼ranga/
     http://www.cisco.com/
     http://www.marathon.org/story/
     http://www.gnu.org/
MYURLS

To strip the tabs in this example, you can give the << operator a - option.

You can also combine here documents with output redirection as follows:

command > file << delimiter
document
delimiter

If used in this form, the output of command is redirected to the specified file, and the input of command becomes the here document.

For example, you can use the following command to create a file with the short list of URLs given previously:

cat > urls << MYURLS
http://www.csua.berkeley.edu/∼ranga/
     http://www.cisco.com/
     http://www.marathon.org/story/
     http://www.gnu.org/
MYURLS


Previous Table of Contents Next