start page | rating of books | rating of authors | reviews | copyrights

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 9.13 Multiline Commands, Secondary Prompts Chapter 9
Saving Time on the Command Line
Next: 9.15 Throwaway Scripts for Complicated Commands
 

9.14 Using Here Documents for Form Letters, etc.

The here document operator << (8.18) is used in shell scripts. It tells the shell to take lines from the script as standard input to a command. The example below shows a loop (45.17) that prints three nasty form letters with the lpr (43.2) command. Each letter has a different person's name and the current date at the top. You can put this loop into a shell script (44.2) or just type it in at a Bourne shell prompt (9.12, 9.13). Each line of the loop body starts with a TAB character, which the <<- operator removes before the printer gets the text:

for person in "Mary Smith" "Doug Jones" "Alison Eddy"
do
   lpr <<- ENDMSG

   `date`

   Dear $person,

   This is your last notice. Buy me pizza tonight or
   else I'll type "rm -r *" when you're not looking.

   This is not a joak.

   Signed,
   The midnight skulker
   ENDMSG
done

Warning! This loop runs three lpr commands; each form letter prints on a separate page. The shell reads the standard input until it finds the terminator word, which in this case is ENDMSG. The word (ENDMSG) has to be on a line all by itself. (Some Bourne shells don't have the <<- operator to remove leading TAB characters. In that case, use << and don't indent the loop body.) The backquotes (9.16) run the date (51.10) command and output its date; $person is replaced with the person's name set at the top of the loop. The rest of the text is copied as is to the standard input of the lpr command.

- JP


Previous: 9.13 Multiline Commands, Secondary Prompts UNIX Power ToolsNext: 9.15 Throwaway Scripts for Complicated Commands
9.13 Multiline Commands, Secondary Prompts Book Index9.15 Throwaway Scripts for Complicated Commands

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System