DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Programming with the UNIX system shell

Here documents

A here document allows you to place into a shell program lines that are redirected to be the input to a command in that program. By using a here document, you can provide input to a command in a shell program without using a separate file. The notation consists of the redirection symbol ``<<'' and a delimiter that specifies the beginning and end of the lines of input. The delimiter can be one character or a string of characters; the ``!'' is often used.

``Format of a here document'' shows the general format for a here document.

   

command <<delimiter . . . input lines . . . delimiter

Format of a here document

In the next example, the program gbday uses a here document to send a generic birthday greeting by redirecting lines of input into the mail command:

   $ cat gbday
   mail $1 <<!
   Best wishes to you on your birthday.
   !
   $
When you use this command, you must specify the recipient's login as the argument to the command. The input included with the use of the here document is:
   Best wishes to you on your birthday.
For example, to send this greeting to the owner of login mary, type:
   $ gbday mary
User mary will receive your greeting the next time she reads her mail messages:
   $ mail
   From mylogin Mon May 14 14:31 CDT 1991
   Best wishes to you on your birthday.
   $

Using ed in a shell program

The here document offers a convenient and useful way to use ed in a shell script. For example, suppose you want to make a shell program that will enter the ed editor, make a global substitution to a file, write the file, and then quit ed. The following screen shows the contents of a program called ch.text which does these tasks.

   $ cat ch.text
   echo Type in the filename.
   read file1
   echo Type in the exact text to be changed.
   read old_text
   echo Type in the exact new text to replace the above.
   read new_text
   ed - $file1 <<!
   g/$old_text/s//$new_text/g
   w
   q
   !
   $
Notice the - (minus) option to the ed command. This option prevents the character count from being displayed on the screen. Notice, also, the format of the ed command for global substitution:
   g/old_text/s//new_text/g
The program uses three variables: file1, old_text, and new_text. When the program is run, it uses the read command to obtain the values of these variables. The variables provide the following information:

file
the name of the file to be edited

old_text
the exact text to be changed

new_text
the new text
Once the variables are entered in the program, the here document redirects the global substitution, the write command, and the quit command into the ed command. Try the new ch.text command. The following screen shows sample responses to the program prompts:
   $ ch.text
   Type in the filename.
   memo
   Type in the exact text to be changed.
   Dear John:
   Type in the exact new text to replace the above.
   To whom it may concern:
   $ cat memo
   To whom it may concern:
   $
Notice that by running the cat command on the changed file, you could examine the results of the global substitution.

The stream editor sed can also be used in shell programming.


Next topic: Return codes
Previous topic: Comments

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004