Debian Codegraph @

HTML/here-docs.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Here Documents</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
9 "><LINK
10 REL="HOME"
11 TITLE="Advanced Bash-Scripting Guide"
12 HREF="index.html"><LINK
13 REL="UP"
14 TITLE="Advanced Topics"
15 HREF="part5.html"><LINK
16 REL="PREVIOUS"
17 TITLE="Globbing"
18 HREF="globbingref.html"><LINK
19 REL="NEXT"
20 TITLE="I/O Redirection"
21 HREF="io-redirection.html"><META
22 HTTP-EQUIV="Content-Style-Type"
23 CONTENT="text/css"><LINK
24 REL="stylesheet"
25 HREF="common/kde-common.css"
26 TYPE="text/css"><META
27 HTTP-EQUIV="Content-Type"
28 CONTENT="text/html; charset=iso-8859-1"><META
29 HTTP-EQUIV="Content-Language"
30 CONTENT="en"><LINK
31 REL="stylesheet"
32 HREF="common/kde-localised.css"
33 TYPE="text/css"
34 TITLE="KDE-English"><LINK
35 REL="stylesheet"
36 HREF="common/kde-default.css"
37 TYPE="text/css"
38 TITLE="KDE-Default"></HEAD
39 ><BODY
40 CLASS="CHAPTER"
41 BGCOLOR="#FFFFFF"
42 TEXT="#000000"
43 LINK="#AA0000"
44 VLINK="#AA0055"
45 ALINK="#AA0000"
46 STYLE="font-family: sans-serif;"
47 ><DIV
48 CLASS="NAVHEADER"
49 ><TABLE
50 SUMMARY="Header navigation table"
51 WIDTH="100%"
52 BORDER="0"
53 CELLPADDING="0"
54 CELLSPACING="0"
55 ><TR
56 ><TH
57 COLSPAN="3"
58 ALIGN="center"
59 >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
60 ></TR
61 ><TR
62 ><TD
63 WIDTH="10%"
64 ALIGN="left"
65 VALIGN="bottom"
66 ><A
67 HREF="globbingref.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 ></TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="io-redirection.html"
82 ACCESSKEY="N"
83 >Next</A
84 ></TD
85 ></TR
86 ></TABLE
87 ><HR
88 ALIGN="LEFT"
89 WIDTH="100%"></DIV
90 ><DIV
91 CLASS="CHAPTER"
92 ><H1
93 ><A
94 NAME="HERE-DOCS"
95 ></A
96 >Chapter 19. Here Documents</H1
97 ><TABLE
98 BORDER="0"
99 WIDTH="100%"
100 CELLSPACING="0"
101 CELLPADDING="0"
102 CLASS="EPIGRAPH"
103 ><TR
104 ><TD
105 WIDTH="45%"
106 >&nbsp;</TD
107 ><TD
108 WIDTH="45%"
109 ALIGN="LEFT"
110 VALIGN="TOP"
111 ><I
112 ><P
113 ><I
114 >Here and now, boys.</I
115 ></P
116 ><P
117 ><I
118 >--Aldous Huxley, <I
119 CLASS="FIRSTTERM"
120 >Island</I
121 ></I
122 ></P
123 ></I
124 ></TD
125 ></TR
126 ></TABLE
127 ><P
128 ><A
129 NAME="HEREDOCREF"
130 ></A
131 ></P
132 ><P
133 >A <I
134 CLASS="FIRSTTERM"
135 >here document</I
136 > is a special-purpose
137 code block. It uses a form of <A
138 HREF="io-redirection.html#IOREDIRREF"
139 >I/O
140 redirection</A
141 > to feed a command list to
142 an interactive program or a command, such as <A
143 HREF="communications.html#FTPREF"
144 >ftp</A
145 >, <A
146 HREF="external.html#CATREF"
147 >cat</A
148 >,
149 or the <I
150 CLASS="FIRSTTERM"
151 >ex</I
152 > text editor.</P
153 ><P
154 ><TABLE
155 BORDER="0"
156 BGCOLOR="#E0E0E0"
157 WIDTH="100%"
158 ><TR
159 ><TD
160 ><PRE
161 CLASS="PROGRAMLISTING"
162 > 1&nbsp;COMMAND &#60;&#60;InputComesFromHERE
163 2&nbsp;...
164 3&nbsp;...
165 4&nbsp;...
166 5&nbsp;InputComesFromHERE</PRE
167 ></TD
168 ></TR
169 ></TABLE
170 ></P
171 ><P
172 ><A
173 NAME="LIMITSTRINGREF"
174 ></A
175 ></P
176 ><P
177 >A <I
178 CLASS="FIRSTTERM"
179 >limit string</I
180 > delineates (frames)
181 the command list. The special symbol <SPAN
182 CLASS="TOKEN"
183 >&#60;&#60;</SPAN
184 > precedes
185 the limit string. This has the effect of redirecting the output
186 of a command block into the <TT
187 CLASS="FILENAME"
188 >stdin</TT
189 > of the program
190 or command. It is similar to <TT
191 CLASS="USERINPUT"
192 ><B
193 >interactive-program &#60;
194 command-file</B
195 ></TT
196 >, where <TT
197 CLASS="FILENAME"
198 >command-file</TT
199 >
200 contains
201
202 <TABLE
203 BORDER="0"
204 BGCOLOR="#E0E0E0"
205 WIDTH="100%"
206 ><TR
207 ><TD
208 ><PRE
209 CLASS="PROGRAMLISTING"
210 > 1&nbsp;command #1
211 2&nbsp;command #2
212 3&nbsp;...</PRE
213 ></TD
214 ></TR
215 ></TABLE
216 ></P
217 ><P
218 >The <I
219 CLASS="FIRSTTERM"
220 >here document</I
221 > equivalent looks
222 like this:
223
224 <TABLE
225 BORDER="0"
226 BGCOLOR="#E0E0E0"
227 WIDTH="100%"
228 ><TR
229 ><TD
230 ><PRE
231 CLASS="PROGRAMLISTING"
232 > 1&nbsp;interactive-program &#60;&#60;LimitString
233 2&nbsp;command #1
234 3&nbsp;command #2
235 4&nbsp;...
236 5&nbsp;LimitString</PRE
237 ></TD
238 ></TR
239 ></TABLE
240 ></P
241 ><P
242 >Choose a <I
243 CLASS="FIRSTTERM"
244 >limit string</I
245 > sufficiently
246 unusual that it will not occur anywhere in the command list and
247 confuse matters.</P
248 ><P
249 >Note that <I
250 CLASS="FIRSTTERM"
251 >here documents</I
252 > may sometimes
253 be used to good effect with non-interactive utilities and commands,
254 such as, for example, <A
255 HREF="system.html#WALLREF"
256 >wall</A
257 >.</P
258 ><DIV
259 CLASS="EXAMPLE"
260 ><HR><A
261 NAME="EX70"
262 ></A
263 ><P
264 ><B
265 >Example 19-1. <I
266 CLASS="FIRSTTERM"
267 >broadcast</I
268 >: Sends message to everyone
269 logged in</B
270 ></P
271 ><TABLE
272 BORDER="0"
273 BGCOLOR="#E0E0E0"
274 WIDTH="100%"
275 ><TR
276 ><TD
277 ><PRE
278 CLASS="PROGRAMLISTING"
279 > 1&nbsp;#!/bin/bash
280 2&nbsp;
281 3&nbsp;wall &#60;&#60;zzz23EndOfMessagezzz23
282 4&nbsp;E-mail your noontime orders for pizza to the system administrator.
283 5&nbsp; (Add an extra dollar for anchovy or mushroom topping.)
284 6&nbsp;# Additional message text goes here.
285 7&nbsp;# Note: 'wall' prints comment lines.
286 8&nbsp;zzz23EndOfMessagezzz23
287 9&nbsp;
288 10&nbsp;# Could have been done more efficiently by
289 11&nbsp;# wall &#60;message-file
290 12&nbsp;# However, embedding the message template in a script
291 13&nbsp;#+ is a quick-and-dirty one-off solution.
292 14&nbsp;
293 15&nbsp;exit</PRE
294 ></TD
295 ></TR
296 ></TABLE
297 ><HR></DIV
298 ><P
299 ><A
300 NAME="VIHERE"
301 ></A
302 ></P
303 ><P
304 >Even such unlikely candidates as the <I
305 CLASS="FIRSTTERM"
306 >vi</I
307 >
308 text editor lend themselves to <I
309 CLASS="FIRSTTERM"
310 >here
311 documents</I
312 >.</P
313 ><DIV
314 CLASS="EXAMPLE"
315 ><HR><A
316 NAME="EX69"
317 ></A
318 ><P
319 ><B
320 >Example 19-2. <I
321 CLASS="FIRSTTERM"
322 >dummyfile</I
323 >: Creates a 2-line dummy
324 file</B
325 ></P
326 ><TABLE
327 BORDER="0"
328 BGCOLOR="#E0E0E0"
329 WIDTH="100%"
330 ><TR
331 ><TD
332 ><PRE
333 CLASS="PROGRAMLISTING"
334 > 1&nbsp;#!/bin/bash
335 2&nbsp;
336 3&nbsp;# Noninteractive use of 'vi' to edit a file.
337 4&nbsp;# Emulates 'sed'.
338 5&nbsp;
339 6&nbsp;E_BADARGS=85
340 7&nbsp;
341 8&nbsp;if [ -z "$1" ]
342 9&nbsp;then
343 10&nbsp; echo "Usage: `basename $0` filename"
344 11&nbsp; exit $E_BADARGS
345 12&nbsp;fi
346 13&nbsp;
347 14&nbsp;TARGETFILE=$1
348 15&nbsp;
349 16&nbsp;# Insert 2 lines in file, then save.
350 17&nbsp;#--------Begin here document-----------#
351 18&nbsp;vi $TARGETFILE &#60;&#60;x23LimitStringx23
352 19&nbsp;i
353 20&nbsp;This is line 1 of the example file.
354 21&nbsp;This is line 2 of the example file.
355 22&nbsp;^[
356 23&nbsp;ZZ
357 24&nbsp;x23LimitStringx23
358 25&nbsp;#----------End here document-----------#
359 26&nbsp;
360 27&nbsp;# Note that ^[ above is a literal escape
361 28&nbsp;#+ typed by Control-V &#60;Esc&#62;.
362 29&nbsp;
363 30&nbsp;# Bram Moolenaar points out that this may not work with 'vim'
364 31&nbsp;#+ because of possible problems with terminal interaction.
365 32&nbsp;
366 33&nbsp;exit</PRE
367 ></TD
368 ></TR
369 ></TABLE
370 ><HR></DIV
371 ><P
372 > The above script could just as effectively have been implemented with
373 <B
374 CLASS="COMMAND"
375 >ex</B
376 >, rather than
377 <B
378 CLASS="COMMAND"
379 >vi</B
380 >. <A
381 NAME="EXSCRIPTREF"
382 ></A
383 ><I
384 CLASS="FIRSTTERM"
385 >Here
386 documents</I
387 > containing a list of <B
388 CLASS="COMMAND"
389 >ex</B
390 >
391 commands are common enough to form their own category, known as
392 <I
393 CLASS="FIRSTTERM"
394 >ex scripts</I
395 >.
396
397 <TABLE
398 BORDER="0"
399 BGCOLOR="#E0E0E0"
400 WIDTH="100%"
401 ><TR
402 ><TD
403 ><PRE
404 CLASS="PROGRAMLISTING"
405 > 1&nbsp;#!/bin/bash
406 2&nbsp;# Replace all instances of "Smith" with "Jones"
407 3&nbsp;#+ in files with a ".txt" filename suffix.
408 4&nbsp;
409 5&nbsp;ORIGINAL=Smith
410 6&nbsp;REPLACEMENT=Jones
411 7&nbsp;
412 8&nbsp;for word in $(fgrep -l $ORIGINAL *.txt)
413 9&nbsp;do
414 10&nbsp; # -------------------------------------
415 11&nbsp; ex $word &#60;&#60;EOF
416 12&nbsp; :%s/$ORIGINAL/$REPLACEMENT/g
417 13&nbsp; :wq
418 14&nbsp;EOF
419 15&nbsp; # :%s is the "ex" substitution command.
420 16&nbsp; # :wq is write-and-quit.
421 17&nbsp; # -------------------------------------
422 18&nbsp;done</PRE
423 ></TD
424 ></TR
425 ></TABLE
426 >
427 </P
428 ><P
429 ><A
430 NAME="CATSCRIPTREF"
431 ></A
432 ></P
433 ><P
434 >Analogous to <SPAN
435 CLASS="QUOTE"
436 >"ex scripts"</SPAN
437 > are <I
438 CLASS="FIRSTTERM"
439 >cat
440 scripts</I
441 >.</P
442 ><DIV
443 CLASS="EXAMPLE"
444 ><HR><A
445 NAME="EX71"
446 ></A
447 ><P
448 ><B
449 >Example 19-3. Multi-line message using <I
450 CLASS="FIRSTTERM"
451 >cat</I
452 ></B
453 ></P
454 ><TABLE
455 BORDER="0"
456 BGCOLOR="#E0E0E0"
457 WIDTH="100%"
458 ><TR
459 ><TD
460 ><PRE
461 CLASS="PROGRAMLISTING"
462 > 1&nbsp;#!/bin/bash
463 2&nbsp;
464 3&nbsp;# 'echo' is fine for printing single line messages,
465 4&nbsp;#+ but somewhat problematic for for message blocks.
466 5&nbsp;# A 'cat' here document overcomes this limitation.
467 6&nbsp;
468 7&nbsp;cat &#60;&#60;End-of-message
469 8&nbsp;-------------------------------------
470 9&nbsp;This is line 1 of the message.
471 10&nbsp;This is line 2 of the message.
472 11&nbsp;This is line 3 of the message.
473 12&nbsp;This is line 4 of the message.
474 13&nbsp;This is the last line of the message.
475 14&nbsp;-------------------------------------
476 15&nbsp;End-of-message
477 16&nbsp;
478 17&nbsp;# Replacing line 7, above, with
479 18&nbsp;#+ cat &#62; $Newfile &#60;&#60;End-of-message
480 19&nbsp;#+ ^^^^^^^^^^
481 20&nbsp;#+ writes the output to the file $Newfile, rather than to stdout.
482 21&nbsp;
483 22&nbsp;exit 0
484 23&nbsp;
485 24&nbsp;
486 25&nbsp;#--------------------------------------------
487 26&nbsp;# Code below disabled, due to "exit 0" above.
488 27&nbsp;
489 28&nbsp;# S.C. points out that the following also works.
490 29&nbsp;echo "-------------------------------------
491 30&nbsp;This is line 1 of the message.
492 31&nbsp;This is line 2 of the message.
493 32&nbsp;This is line 3 of the message.
494 33&nbsp;This is line 4 of the message.
495 34&nbsp;This is the last line of the message.
496 35&nbsp;-------------------------------------"
497 36&nbsp;# However, text may not include double quotes unless they are escaped.</PRE
498 ></TD
499 ></TR
500 ></TABLE
501 ><HR></DIV
502 ><P
503 ><A
504 NAME="LIMITSTRDASH"
505 ></A
506 ></P
507 ><P
508 >The <TT
509 CLASS="OPTION"
510 >-</TT
511 > option to mark a here document limit string
512 (<TT
513 CLASS="USERINPUT"
514 ><B
515 >&#60;&#60;-LimitString</B
516 ></TT
517 >) suppresses leading
518 tabs (but not spaces) in the output. This may be useful in making
519 a script more readable.</P
520 ><DIV
521 CLASS="EXAMPLE"
522 ><HR><A
523 NAME="EX71A"
524 ></A
525 ><P
526 ><B
527 >Example 19-4. Multi-line message, with tabs suppressed</B
528 ></P
529 ><TABLE
530 BORDER="0"
531 BGCOLOR="#E0E0E0"
532 WIDTH="100%"
533 ><TR
534 ><TD
535 ><PRE
536 CLASS="PROGRAMLISTING"
537 > 1&nbsp;#!/bin/bash
538 2&nbsp;# Same as previous example, but...
539 3&nbsp;
540 4&nbsp;# The - option to a here document &#60;&#60;-
541 5&nbsp;#+ suppresses leading tabs in the body of the document,
542 6&nbsp;#+ but *not* spaces.
543 7&nbsp;
544 8&nbsp;cat &#60;&#60;-ENDOFMESSAGE
545 9&nbsp; This is line 1 of the message.
546 10&nbsp; This is line 2 of the message.
547 11&nbsp; This is line 3 of the message.
548 12&nbsp; This is line 4 of the message.
549 13&nbsp; This is the last line of the message.
550 14&nbsp;ENDOFMESSAGE
551 15&nbsp;# The output of the script will be flush left.
552 16&nbsp;# Leading tab in each line will not show.
553 17&nbsp;
554 18&nbsp;# Above 5 lines of "message" prefaced by a tab, not spaces.
555 19&nbsp;# Spaces not affected by &#60;&#60;- .
556 20&nbsp;
557 21&nbsp;# Note that this option has no effect on *embedded* tabs.
558 22&nbsp;
559 23&nbsp;exit 0</PRE
560 ></TD
561 ></TR
562 ></TABLE
563 ><HR></DIV
564 ><P
565 ><A
566 NAME="HEREPASSP"
567 ></A
568 ></P
569 ><P
570 >A <I
571 CLASS="FIRSTTERM"
572 >here document</I
573 > supports parameter and
574 command substitution. It is therefore possible to pass different
575 parameters to the body of the here document, changing its output
576 accordingly.</P
577 ><DIV
578 CLASS="EXAMPLE"
579 ><HR><A
580 NAME="EX71B"
581 ></A
582 ><P
583 ><B
584 >Example 19-5. Here document with replaceable parameters</B
585 ></P
586 ><TABLE
587 BORDER="0"
588 BGCOLOR="#E0E0E0"
589 WIDTH="100%"
590 ><TR
591 ><TD
592 ><PRE
593 CLASS="PROGRAMLISTING"
594 > 1&nbsp;#!/bin/bash
595 2&nbsp;# Another 'cat' here document, using parameter substitution.
596 3&nbsp;
597 4&nbsp;# Try it with no command-line parameters, ./scriptname
598 5&nbsp;# Try it with one command-line parameter, ./scriptname Mortimer
599 6&nbsp;# Try it with one two-word quoted command-line parameter,
600 7&nbsp;# ./scriptname "Mortimer Jones"
601 8&nbsp;
602 9&nbsp;CMDLINEPARAM=1 # Expect at least command-line parameter.
603 10&nbsp;
604 11&nbsp;if [ $# -ge $CMDLINEPARAM ]
605 12&nbsp;then
606 13&nbsp; NAME=$1 # If more than one command-line param,
607 14&nbsp; #+ then just take the first.
608 15&nbsp;else
609 16&nbsp; NAME="John Doe" # Default, if no command-line parameter.
610 17&nbsp;fi
611 18&nbsp;
612 19&nbsp;RESPONDENT="the author of this fine script"
613 20&nbsp;
614 21&nbsp;
615 22&nbsp;cat &#60;&#60;Endofmessage
616 23&nbsp;
617 24&nbsp;Hello, there, $NAME.
618 25&nbsp;Greetings to you, $NAME, from $RESPONDENT.
619 26&nbsp;
620 27&nbsp;# This comment shows up in the output (why?).
621 28&nbsp;
622 29&nbsp;Endofmessage
623 30&nbsp;
624 31&nbsp;# Note that the blank lines show up in the output.
625 32&nbsp;# So does the comment.
626 33&nbsp;
627 34&nbsp;exit</PRE
628 ></TD
629 ></TR
630 ></TABLE
631 ><HR></DIV
632 ><P
633 ><A
634 NAME="HEREPARAMSUB"
635 ></A
636 ></P
637 ><P
638 >This is a useful script containing a <I
639 CLASS="FIRSTTERM"
640 >here
641 document</I
642 > with parameter substitution.</P
643 ><DIV
644 CLASS="EXAMPLE"
645 ><HR><A
646 NAME="EX72"
647 ></A
648 ><P
649 ><B
650 >Example 19-6. Upload a file pair to <I
651 CLASS="FIRSTTERM"
652 >Sunsite</I
653 > incoming
654 directory</B
655 ></P
656 ><TABLE
657 BORDER="0"
658 BGCOLOR="#E0E0E0"
659 WIDTH="100%"
660 ><TR
661 ><TD
662 ><PRE
663 CLASS="PROGRAMLISTING"
664 > 1&nbsp;#!/bin/bash
665 2&nbsp;# upload.sh
666 3&nbsp;
667 4&nbsp;# Upload file pair (Filename.lsm, Filename.tar.gz)
668 5&nbsp;#+ to incoming directory at Sunsite/UNC (ibiblio.org).
669 6&nbsp;# Filename.tar.gz is the tarball itself.
670 7&nbsp;# Filename.lsm is the descriptor file.
671 8&nbsp;# Sunsite requires "lsm" file, otherwise will bounce contributions.
672 9&nbsp;
673 10&nbsp;
674 11&nbsp;E_ARGERROR=85
675 12&nbsp;
676 13&nbsp;if [ -z "$1" ]
677 14&nbsp;then
678 15&nbsp; echo "Usage: `basename $0` Filename-to-upload"
679 16&nbsp; exit $E_ARGERROR
680 17&nbsp;fi
681 18&nbsp;
682 19&nbsp;
683 20&nbsp;Filename=`basename $1` # Strips pathname out of file name.
684 21&nbsp;
685 22&nbsp;Server="ibiblio.org"
686 23&nbsp;Directory="/incoming/Linux"
687 24&nbsp;# These need not be hard-coded into script,
688 25&nbsp;#+ but may instead be changed to command-line argument.
689 26&nbsp;
690 27&nbsp;Password="your.e-mail.address" # Change above to suit.
691 28&nbsp;
692 29&nbsp;ftp -n $Server &#60;&#60;End-Of-Session
693 30&nbsp;# -n option disables auto-logon
694 31&nbsp;
695 32&nbsp;user anonymous "$Password" # If this doesn't work, then try:
696 33&nbsp; # quote user anonymous "$Password"
697 34&nbsp;binary
698 35&nbsp;bell # Ring 'bell' after each file transfer.
699 36&nbsp;cd $Directory
700 37&nbsp;put "$Filename.lsm"
701 38&nbsp;put "$Filename.tar.gz"
702 39&nbsp;bye
703 40&nbsp;End-Of-Session
704 41&nbsp;
705 42&nbsp;exit 0</PRE
706 ></TD
707 ></TR
708 ></TABLE
709 ><HR></DIV
710 ><P
711 ><A
712 NAME="HEREESC"
713 ></A
714 ></P
715 ><P
716 >Quoting or escaping the <SPAN
717 CLASS="QUOTE"
718 >"limit string"</SPAN
719 > at the
720 head of a here document disables parameter substitution within its
721 body. The reason for this is that <I
722 CLASS="FIRSTTERM"
723 >quoting/escaping the
724 limit string</I
725 > effectively <A
726 HREF="escapingsection.html#ESCP"
727 >escapes</A
728 > the <SPAN
729 CLASS="TOKEN"
730 >$</SPAN
731 >,
732 <SPAN
733 CLASS="TOKEN"
734 >`</SPAN
735 >, and <SPAN
736 CLASS="TOKEN"
737 >\</SPAN
738 > <A
739 HREF="special-chars.html#SCHARLIST"
740 >special characters</A
741 >, and causes them to
742 be interpreted literally. (Thank you, Allen Halsey, for pointing
743 this out.)</P
744 ><DIV
745 CLASS="EXAMPLE"
746 ><HR><A
747 NAME="EX71C"
748 ></A
749 ><P
750 ><B
751 >Example 19-7. Parameter substitution turned off</B
752 ></P
753 ><TABLE
754 BORDER="0"
755 BGCOLOR="#E0E0E0"
756 WIDTH="100%"
757 ><TR
758 ><TD
759 ><PRE
760 CLASS="PROGRAMLISTING"
761 > 1&nbsp;#!/bin/bash
762 2&nbsp;# A 'cat' here-document, but with parameter substitution disabled.
763 3&nbsp;
764 4&nbsp;NAME="John Doe"
765 5&nbsp;RESPONDENT="the author of this fine script"
766 6&nbsp;
767 7&nbsp;cat &#60;&#60;'Endofmessage'
768 8&nbsp;
769 9&nbsp;Hello, there, $NAME.
770 10&nbsp;Greetings to you, $NAME, from $RESPONDENT.
771 11&nbsp;
772 12&nbsp;Endofmessage
773 13&nbsp;
774 14&nbsp;# No parameter substitution when the "limit string" is quoted or escaped.
775 15&nbsp;# Either of the following at the head of the here document would have
776 16&nbsp;#+ the same effect.
777 17&nbsp;# cat &#60;&#60;"Endofmessage"
778 18&nbsp;# cat &#60;&#60;\Endofmessage
779 19&nbsp;
780 20&nbsp;
781 21&nbsp;
782 22&nbsp;# And, likewise:
783 23&nbsp;
784 24&nbsp;cat &#60;&#60;"SpecialCharTest"
785 25&nbsp;
786 26&nbsp;Directory listing would follow
787 27&nbsp;if limit string were not quoted.
788 28&nbsp;`ls -l`
789 29&nbsp;
790 30&nbsp;Arithmetic expansion would take place
791 31&nbsp;if limit string were not quoted.
792 32&nbsp;$((5 + 3))
793 33&nbsp;
794 34&nbsp;A a single backslash would echo
795 35&nbsp;if limit string were not quoted.
796 36&nbsp;\\
797 37&nbsp;
798 38&nbsp;SpecialCharTest
799 39&nbsp;
800 40&nbsp;
801 41&nbsp;exit</PRE
802 ></TD
803 ></TR
804 ></TABLE
805 ><HR></DIV
806 ><P
807 ><A
808 NAME="HERELIT"
809 ></A
810 ></P
811 ><P
812 >Disabling parameter substitution permits outputting literal text.
813 Generating scripts or even program code is one use for this.</P
814 ><DIV
815 CLASS="EXAMPLE"
816 ><HR><A
817 NAME="GENERATESCRIPT"
818 ></A
819 ><P
820 ><B
821 >Example 19-8. A script that generates another script</B
822 ></P
823 ><TABLE
824 BORDER="0"
825 BGCOLOR="#E0E0E0"
826 WIDTH="100%"
827 ><TR
828 ><TD
829 ><PRE
830 CLASS="PROGRAMLISTING"
831 > 1&nbsp;#!/bin/bash
832 2&nbsp;# generate-script.sh
833 3&nbsp;# Based on an idea by Albert Reiner.
834 4&nbsp;
835 5&nbsp;OUTFILE=generated.sh # Name of the file to generate.
836 6&nbsp;
837 7&nbsp;
838 8&nbsp;# -----------------------------------------------------------
839 9&nbsp;# 'Here document containing the body of the generated script.
840 10&nbsp;(
841 11&nbsp;cat &#60;&#60;'EOF'
842 12&nbsp;#!/bin/bash
843 13&nbsp;
844 14&nbsp;echo "This is a generated shell script."
845 15&nbsp;# Note that since we are inside a subshell,
846 16&nbsp;#+ we can't access variables in the "outside" script.
847 17&nbsp;
848 18&nbsp;echo "Generated file will be named: $OUTFILE"
849 19&nbsp;# Above line will not work as normally expected
850 20&nbsp;#+ because parameter expansion has been disabled.
851 21&nbsp;# Instead, the result is literal output.
852 22&nbsp;
853 23&nbsp;a=7
854 24&nbsp;b=3
855 25&nbsp;
856 26&nbsp;let "c = $a * $b"
857 27&nbsp;echo "c = $c"
858 28&nbsp;
859 29&nbsp;exit 0
860 30&nbsp;EOF
861 31&nbsp;) &#62; $OUTFILE
862 32&nbsp;# -----------------------------------------------------------
863 33&nbsp;
864 34&nbsp;# Quoting the 'limit string' prevents variable expansion
865 35&nbsp;#+ within the body of the above 'here document.'
866 36&nbsp;# This permits outputting literal strings in the output file.
867 37&nbsp;
868 38&nbsp;if [ -f "$OUTFILE" ]
869 39&nbsp;then
870 40&nbsp; chmod 755 $OUTFILE
871 41&nbsp; # Make the generated file executable.
872 42&nbsp;else
873 43&nbsp; echo "Problem in creating file: \"$OUTFILE\""
874 44&nbsp;fi
875 45&nbsp;
876 46&nbsp;# This method also works for generating
877 47&nbsp;#+ C programs, Perl programs, Python programs, Makefiles,
878 48&nbsp;#+ and the like.
879 49&nbsp;
880 50&nbsp;exit 0</PRE
881 ></TD
882 ></TR
883 ></TABLE
884 ><HR></DIV
885 ><P
886 ><A
887 NAME="HERECS"
888 ></A
889 ></P
890 ><P
891 > It is possible to set a variable from the output of a here document.
892 This is actually a devious form of <A
893 HREF="commandsub.html#COMMANDSUBREF"
894 >command substitution</A
895 >.
896 <TABLE
897 BORDER="0"
898 BGCOLOR="#E0E0E0"
899 WIDTH="100%"
900 ><TR
901 ><TD
902 ><PRE
903 CLASS="PROGRAMLISTING"
904 > 1&nbsp;variable=$(cat &#60;&#60;SETVAR
905 2&nbsp;This variable
906 3&nbsp;runs over multiple lines.
907 4&nbsp;SETVAR
908 5&nbsp;)
909 6&nbsp;
910 7&nbsp;echo "$variable"</PRE
911 ></TD
912 ></TR
913 ></TABLE
914 >
915 </P
916 ><P
917 ><A
918 NAME="HEREFUNC"
919 ></A
920 ></P
921 ><P
922 >A here document can supply input to a function in the same
923 script.</P
924 ><DIV
925 CLASS="EXAMPLE"
926 ><HR><A
927 NAME="HF"
928 ></A
929 ><P
930 ><B
931 >Example 19-9. Here documents and functions</B
932 ></P
933 ><TABLE
934 BORDER="0"
935 BGCOLOR="#E0E0E0"
936 WIDTH="100%"
937 ><TR
938 ><TD
939 ><PRE
940 CLASS="PROGRAMLISTING"
941 > 1&nbsp;#!/bin/bash
942 2&nbsp;# here-function.sh
943 3&nbsp;
944 4&nbsp;GetPersonalData ()
945 5&nbsp;{
946 6&nbsp; read firstname
947 7&nbsp; read lastname
948 8&nbsp; read address
949 9&nbsp; read city
950 10&nbsp; read state
951 11&nbsp; read zipcode
952 12&nbsp;} # This certainly appears to be an interactive function, but . . .
953 13&nbsp;
954 14&nbsp;
955 15&nbsp;# Supply input to the above function.
956 16&nbsp;GetPersonalData &#60;&#60;RECORD001
957 17&nbsp;Bozo
958 18&nbsp;Bozeman
959 19&nbsp;2726 Nondescript Dr.
960 20&nbsp;Bozeman
961 21&nbsp;MT
962 22&nbsp;21226
963 23&nbsp;RECORD001
964 24&nbsp;
965 25&nbsp;
966 26&nbsp;echo
967 27&nbsp;echo "$firstname $lastname"
968 28&nbsp;echo "$address"
969 29&nbsp;echo "$city, $state $zipcode"
970 30&nbsp;echo
971 31&nbsp;
972 32&nbsp;exit 0</PRE
973 ></TD
974 ></TR
975 ></TABLE
976 ><HR></DIV
977 ><P
978 ><A
979 NAME="ANONHEREDOC0"
980 ></A
981 ></P
982 ><P
983 >It is possible to use <SPAN
984 CLASS="TOKEN"
985 >:</SPAN
986 > as a dummy command
987 accepting output from a here document. This, in effect, creates an
988 <SPAN
989 CLASS="QUOTE"
990 >"anonymous"</SPAN
991 > here document.</P
992 ><DIV
993 CLASS="EXAMPLE"
994 ><HR><A
995 NAME="ANONHEREDOC"
996 ></A
997 ><P
998 ><B
999 >Example 19-10. <SPAN
1000 CLASS="QUOTE"
1001 >"Anonymous"</SPAN
1002 > Here Document</B
1003 ></P
1004 ><TABLE
1005 BORDER="0"
1006 BGCOLOR="#E0E0E0"
1007 WIDTH="100%"
1008 ><TR
1009 ><TD
1010 ><PRE
1011 CLASS="PROGRAMLISTING"
1012 > 1&nbsp;#!/bin/bash
1013 2&nbsp;
1014 3&nbsp;: &#60;&#60;TESTVARIABLES
1015 4&nbsp;${HOSTNAME?}${USER?}${MAIL?} # Print error message if one of the variables not set.
1016 5&nbsp;TESTVARIABLES
1017 6&nbsp;
1018 7&nbsp;exit $?</PRE
1019 ></TD
1020 ></TR
1021 ></TABLE
1022 ><HR></DIV
1023 ><P
1024 ><A
1025 NAME="CBLOCK1"
1026 ></A
1027 ></P
1028 ><DIV
1029 CLASS="TIP"
1030 ><TABLE
1031 CLASS="TIP"
1032 WIDTH="100%"
1033 BORDER="0"
1034 ><TR
1035 ><TD
1036 WIDTH="25"
1037 ALIGN="CENTER"
1038 VALIGN="TOP"
1039 ><IMG
1040 SRC="common/tip.png"
1041 HSPACE="5"
1042 ALT="Tip"></TD
1043 ><TD
1044 ALIGN="LEFT"
1045 VALIGN="TOP"
1046 ><P
1047 >A variation of the above technique permits <SPAN
1048 CLASS="QUOTE"
1049 >"commenting
1050 out"</SPAN
1051 > blocks of code.</P
1052 ></TD
1053 ></TR
1054 ></TABLE
1055 ></DIV
1056 ><DIV
1057 CLASS="EXAMPLE"
1058 ><HR><A
1059 NAME="COMMENTBLOCK"
1060 ></A
1061 ><P
1062 ><B
1063 >Example 19-11. Commenting out a block of code</B
1064 ></P
1065 ><TABLE
1066 BORDER="0"
1067 BGCOLOR="#E0E0E0"
1068 WIDTH="100%"
1069 ><TR
1070 ><TD
1071 ><PRE
1072 CLASS="PROGRAMLISTING"
1073 > 1&nbsp;#!/bin/bash
1074 2&nbsp;# commentblock.sh
1075 3&nbsp;
1076 4&nbsp;: &#60;&#60;COMMENTBLOCK
1077 5&nbsp;echo "This line will not echo."
1078 6&nbsp;This is a comment line missing the "#" prefix.
1079 7&nbsp;This is another comment line missing the "#" prefix.
1080 8&nbsp;
1081 9&nbsp;&#38;*@!!++=
1082 10&nbsp;The above line will cause no error message,
1083 11&nbsp;because the Bash interpreter will ignore it.
1084 12&nbsp;COMMENTBLOCK
1085 13&nbsp;
1086 14&nbsp;echo "Exit value of above \"COMMENTBLOCK\" is $?." # 0
1087 15&nbsp;# No error shown.
1088 16&nbsp;echo
1089 17&nbsp;
1090 18&nbsp;
1091 19&nbsp;# The above technique also comes in useful for commenting out
1092 20&nbsp;#+ a block of working code for debugging purposes.
1093 21&nbsp;# This saves having to put a "#" at the beginning of each line,
1094 22&nbsp;#+ then having to go back and delete each "#" later.
1095 23&nbsp;# Note that the use of of colon, above, is optional.
1096 24&nbsp;
1097 25&nbsp;echo "Just before commented-out code block."
1098 26&nbsp;# The lines of code between the double-dashed lines will not execute.
1099 27&nbsp;# ===================================================================
1100 28&nbsp;: &#60;&#60;DEBUGXXX
1101 29&nbsp;for file in *
1102 30&nbsp;do
1103 31&nbsp; cat "$file"
1104 32&nbsp;done
1105 33&nbsp;DEBUGXXX
1106 34&nbsp;# ===================================================================
1107 35&nbsp;echo "Just after commented-out code block."
1108 36&nbsp;
1109 37&nbsp;exit 0
1110 38&nbsp;
1111 39&nbsp;
1112 40&nbsp;
1113 41&nbsp;######################################################################
1114 42&nbsp;# Note, however, that if a bracketed variable is contained within
1115 43&nbsp;#+ the commented-out code block,
1116 44&nbsp;#+ then this could cause problems.
1117 45&nbsp;# for example:
1118 46&nbsp;
1119 47&nbsp;
1120 48&nbsp;#/!/bin/bash
1121 49&nbsp;
1122 50&nbsp; : &#60;&#60;COMMENTBLOCK
1123 51&nbsp; echo "This line will not echo."
1124 52&nbsp; &#38;*@!!++=
1125 53&nbsp; ${foo_bar_bazz?}
1126 54&nbsp; $(rm -rf /tmp/foobar/)
1127 55&nbsp; $(touch my_build_directory/cups/Makefile)
1128 56&nbsp;COMMENTBLOCK
1129 57&nbsp;
1130 58&nbsp;
1131 59&nbsp;$ sh commented-bad.sh
1132 60&nbsp;commented-bad.sh: line 3: foo_bar_bazz: parameter null or not set
1133 61&nbsp;
1134 62&nbsp;# The remedy for this is to strong-quote the 'COMMENTBLOCK' in line 49, above.
1135 63&nbsp;
1136 64&nbsp; : &#60;&#60;'COMMENTBLOCK'
1137 65&nbsp;
1138 66&nbsp;# Thank you, Kurt Pfeifle, for pointing this out.</PRE
1139 ></TD
1140 ></TR
1141 ></TABLE
1142 ><HR></DIV
1143 ><P
1144 ><A
1145 NAME="HSELFDOC"
1146 ></A
1147 ></P
1148 ><DIV
1149 CLASS="TIP"
1150 ><TABLE
1151 CLASS="TIP"
1152 WIDTH="100%"
1153 BORDER="0"
1154 ><TR
1155 ><TD
1156 WIDTH="25"
1157 ALIGN="CENTER"
1158 VALIGN="TOP"
1159 ><IMG
1160 SRC="common/tip.png"
1161 HSPACE="5"
1162 ALT="Tip"></TD
1163 ><TD
1164 ALIGN="LEFT"
1165 VALIGN="TOP"
1166 ><P
1167 >Yet another twist of this nifty trick makes
1168 <SPAN
1169 CLASS="QUOTE"
1170 >"self-documenting"</SPAN
1171 > scripts possible.</P
1172 ></TD
1173 ></TR
1174 ></TABLE
1175 ></DIV
1176 ><DIV
1177 CLASS="EXAMPLE"
1178 ><HR><A
1179 NAME="SELFDOCUMENT"
1180 ></A
1181 ><P
1182 ><B
1183 >Example 19-12. A self-documenting script</B
1184 ></P
1185 ><TABLE
1186 BORDER="0"
1187 BGCOLOR="#E0E0E0"
1188 WIDTH="100%"
1189 ><TR
1190 ><TD
1191 ><PRE
1192 CLASS="PROGRAMLISTING"
1193 > 1&nbsp;#!/bin/bash
1194 2&nbsp;# self-document.sh: self-documenting script
1195 3&nbsp;# Modification of "colm.sh".
1196 4&nbsp;
1197 5&nbsp;DOC_REQUEST=70
1198 6&nbsp;
1199 7&nbsp;if [ "$1" = "-h" -o "$1" = "--help" ] # Request help.
1200 8&nbsp;then
1201 9&nbsp; echo; echo "Usage: $0 [directory-name]"; echo
1202 10&nbsp; sed --silent -e '/DOCUMENTATIONXX$/,/^DOCUMENTATIONXX$/p' "$0" |
1203 11&nbsp; sed -e '/DOCUMENTATIONXX$/d'; exit $DOC_REQUEST; fi
1204 12&nbsp;
1205 13&nbsp;
1206 14&nbsp;: &#60;&#60;DOCUMENTATIONXX
1207 15&nbsp;List the statistics of a specified directory in tabular format.
1208 16&nbsp;---------------------------------------------------------------
1209 17&nbsp;The command-line parameter gives the directory to be listed.
1210 18&nbsp;If no directory specified or directory specified cannot be read,
1211 19&nbsp;then list the current working directory.
1212 20&nbsp;
1213 21&nbsp;DOCUMENTATIONXX
1214 22&nbsp;
1215 23&nbsp;if [ -z "$1" -o ! -r "$1" ]
1216 24&nbsp;then
1217 25&nbsp; directory=.
1218 26&nbsp;else
1219 27&nbsp; directory="$1"
1220 28&nbsp;fi
1221 29&nbsp;
1222 30&nbsp;echo "Listing of "$directory":"; echo
1223 31&nbsp;(printf "PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n" \
1224 32&nbsp;; ls -l "$directory" | sed 1d) | column -t
1225 33&nbsp;
1226 34&nbsp;exit 0</PRE
1227 ></TD
1228 ></TR
1229 ></TABLE
1230 ><HR></DIV
1231 ><P
1232 >Using a <A
1233 HREF="here-docs.html#CATSCRIPTREF"
1234 >cat script</A
1235 > is an
1236 alternate way of accomplishing this.</P
1237 ><P
1238 > <TABLE
1239 BORDER="0"
1240 BGCOLOR="#E0E0E0"
1241 WIDTH="100%"
1242 ><TR
1243 ><TD
1244 ><PRE
1245 CLASS="PROGRAMLISTING"
1246 > 1&nbsp;DOC_REQUEST=70
1247 2&nbsp;
1248 3&nbsp;if [ "$1" = "-h" -o "$1" = "--help" ] # Request help.
1249 4&nbsp;then # Use a "cat script" . . .
1250 5&nbsp; cat &#60;&#60;DOCUMENTATIONXX
1251 6&nbsp;List the statistics of a specified directory in tabular format.
1252 7&nbsp;---------------------------------------------------------------
1253 8&nbsp;The command-line parameter gives the directory to be listed.
1254 9&nbsp;If no directory specified or directory specified cannot be read,
1255 10&nbsp;then list the current working directory.
1256 11&nbsp;
1257 12&nbsp;DOCUMENTATIONXX
1258 13&nbsp;exit $DOC_REQUEST
1259 14&nbsp;fi</PRE
1260 ></TD
1261 ></TR
1262 ></TABLE
1263 >
1264 </P
1265 ><P
1266 >See also <A
1267 HREF="contributed-scripts.html#ISSPAMMER2"
1268 >Example A-28</A
1269 >, <A
1270 HREF="contributed-scripts.html#PETALS"
1271 >Example A-40</A
1272 >,
1273 <A
1274 HREF="contributed-scripts.html#QKY"
1275 >Example A-41</A
1276 >, and <A
1277 HREF="contributed-scripts.html#NIM"
1278 >Example A-42</A
1279 > for more examples
1280 of self-documenting scripts.</P
1281 ><P
1282 ><A
1283 NAME="HERETEMP"
1284 ></A
1285 ></P
1286 ><DIV
1287 CLASS="NOTE"
1288 ><TABLE
1289 CLASS="NOTE"
1290 WIDTH="100%"
1291 BORDER="0"
1292 ><TR
1293 ><TD
1294 WIDTH="25"
1295 ALIGN="CENTER"
1296 VALIGN="TOP"
1297 ><IMG
1298 SRC="common/note.png"
1299 HSPACE="5"
1300 ALT="Note"></TD
1301 ><TD
1302 ALIGN="LEFT"
1303 VALIGN="TOP"
1304 ><P
1305 >Here documents create temporary files, but these
1306 files are deleted after opening and are not accessible to
1307 any other process.</P
1308 ><P
1309 > <TABLE
1310 BORDER="0"
1311 BGCOLOR="#E0E0E0"
1312 WIDTH="100%"
1313 ><TR
1314 ><TD
1315 ><PRE
1316 CLASS="SCREEN"
1317 > <TT
1318 CLASS="PROMPT"
1319 >bash$ </TT
1320 ><TT
1321 CLASS="USERINPUT"
1322 ><B
1323 >bash -c 'lsof -a -p $$ -d0' &#60;&#60; EOF</B
1324 ></TT
1325 >
1326 <TT
1327 CLASS="PROMPT"
1328 >&#62; </TT
1329 ><TT
1330 CLASS="USERINPUT"
1331 ><B
1332 >EOF</B
1333 ></TT
1334 >
1335 <TT
1336 CLASS="COMPUTEROUTPUT"
1337 >lsof 1213 bozo 0r REG 3,5 0 30386 /tmp/t1213-0-sh (deleted)</TT
1338 >
1339 </PRE
1340 ></TD
1341 ></TR
1342 ></TABLE
1343 >
1344 </P
1345 ></TD
1346 ></TR
1347 ></TABLE
1348 ></DIV
1349 ><DIV
1350 CLASS="CAUTION"
1351 ><TABLE
1352 CLASS="CAUTION"
1353 WIDTH="100%"
1354 BORDER="0"
1355 ><TR
1356 ><TD
1357 WIDTH="25"
1358 ALIGN="CENTER"
1359 VALIGN="TOP"
1360 ><IMG
1361 SRC="common/caution.png"
1362 HSPACE="5"
1363 ALT="Caution"></TD
1364 ><TD
1365 ALIGN="LEFT"
1366 VALIGN="TOP"
1367 ><P
1368 >Some utilities will not work inside a
1369 <I
1370 CLASS="FIRSTTERM"
1371 >here document</I
1372 >.</P
1373 ></TD
1374 ></TR
1375 ></TABLE
1376 ></DIV
1377 ><P
1378 ><A
1379 NAME="INDENTEDLS"
1380 ></A
1381 ></P
1382 ><DIV
1383 CLASS="WARNING"
1384 ><TABLE
1385 CLASS="WARNING"
1386 WIDTH="100%"
1387 BORDER="0"
1388 ><TR
1389 ><TD
1390 WIDTH="25"
1391 ALIGN="CENTER"
1392 VALIGN="TOP"
1393 ><IMG
1394 SRC="common/warning.png"
1395 HSPACE="5"
1396 ALT="Warning"></TD
1397 ><TD
1398 ALIGN="LEFT"
1399 VALIGN="TOP"
1400 ><P
1401 >The closing <I
1402 CLASS="FIRSTTERM"
1403 >limit string</I
1404 >,
1405 on the final line of a here document, must start in the
1406 <SPAN
1407 CLASS="emphasis"
1408 ><I
1409 CLASS="EMPHASIS"
1410 >first</I
1411 ></SPAN
1412 > character position. There can
1413 be <SPAN
1414 CLASS="emphasis"
1415 ><I
1416 CLASS="EMPHASIS"
1417 >no leading whitespace</I
1418 ></SPAN
1419 >. Trailing
1420 whitespace after the limit string likewise causes unexpected
1421 behavior. The whitespace prevents the limit string from being
1422 recognized.
1423
1424 <A
1425 NAME="AEN17822"
1426 HREF="#FTN.AEN17822"
1427 >[1]</A
1428 >
1429
1430 </P
1431 ><P
1432 > <TABLE
1433 BORDER="0"
1434 BGCOLOR="#E0E0E0"
1435 WIDTH="100%"
1436 ><TR
1437 ><TD
1438 ><PRE
1439 CLASS="PROGRAMLISTING"
1440 > 1&nbsp;#!/bin/bash
1441 2&nbsp;
1442 3&nbsp;echo "----------------------------------------------------------------------"
1443 4&nbsp;
1444 5&nbsp;cat &#60;&#60;LimitString
1445 6&nbsp;echo "This is line 1 of the message inside the here document."
1446 7&nbsp;echo "This is line 2 of the message inside the here document."
1447 8&nbsp;echo "This is the final line of the message inside the here document."
1448 9&nbsp; LimitString
1449 10&nbsp;#^^^^Indented limit string. Error! This script will not behave as expected.
1450 11&nbsp;
1451 12&nbsp;echo "----------------------------------------------------------------------"
1452 13&nbsp;
1453 14&nbsp;# These comments are outside the 'here document',
1454 15&nbsp;#+ and should not echo.
1455 16&nbsp;
1456 17&nbsp;echo "Outside the here document."
1457 18&nbsp;
1458 19&nbsp;exit 0
1459 20&nbsp;
1460 21&nbsp;echo "This line had better not echo." # Follows an 'exit' command.</PRE
1461 ></TD
1462 ></TR
1463 ></TABLE
1464 >
1465 </P
1466 ></TD
1467 ></TR
1468 ></TABLE
1469 ></DIV
1470 ><DIV
1471 CLASS="CAUTION"
1472 ><TABLE
1473 CLASS="CAUTION"
1474 WIDTH="100%"
1475 BORDER="0"
1476 ><TR
1477 ><TD
1478 WIDTH="25"
1479 ALIGN="CENTER"
1480 VALIGN="TOP"
1481 ><IMG
1482 SRC="common/caution.png"
1483 HSPACE="5"
1484 ALT="Caution"></TD
1485 ><TD
1486 ALIGN="LEFT"
1487 VALIGN="TOP"
1488 ><P
1489 ><A
1490 NAME="EXCLLS"
1491 ></A
1492 >Some people very cleverly use a
1493 single <SPAN
1494 CLASS="TOKEN"
1495 >!</SPAN
1496 > as a limit string. But, that's not
1497 necessarily a good idea.</P
1498 ><P
1499 ><TABLE
1500 BORDER="0"
1501 BGCOLOR="#E0E0E0"
1502 WIDTH="100%"
1503 ><TR
1504 ><TD
1505 ><PRE
1506 CLASS="PROGRAMLISTING"
1507 > 1&nbsp;# This works.
1508 2&nbsp;cat &#60;&#60;!
1509 3&nbsp;Hello!
1510 4&nbsp;! Three more exclamations !!!
1511 5&nbsp;!
1512 6&nbsp;
1513 7&nbsp;
1514 8&nbsp;# But . . .
1515 9&nbsp;cat &#60;&#60;!
1516 10&nbsp;Hello!
1517 11&nbsp;Single exclamation point follows!
1518 12&nbsp;!
1519 13&nbsp;!
1520 14&nbsp;# Crashes with an error message.
1521 15&nbsp;
1522 16&nbsp;
1523 17&nbsp;# However, the following will work.
1524 18&nbsp;cat &#60;&#60;EOF
1525 19&nbsp;Hello!
1526 20&nbsp;Single exclamation point follows!
1527 21&nbsp;!
1528 22&nbsp;EOF
1529 23&nbsp;# It's safer to use a multi-character limit string.</PRE
1530 ></TD
1531 ></TR
1532 ></TABLE
1533 ></P
1534 ></TD
1535 ></TR
1536 ></TABLE
1537 ></DIV
1538 ><P
1539 >For those tasks too complex for a <I
1540 CLASS="FIRSTTERM"
1541 >here
1542 document</I
1543 >, consider using the
1544 <TT
1545 CLASS="REPLACEABLE"
1546 ><I
1547 >expect</I
1548 ></TT
1549 > scripting language, which
1550 was specifically designed for feeding input into interactive
1551 programs.</P
1552 ><DIV
1553 CLASS="SECT1"
1554 ><H1
1555 CLASS="SECT1"
1556 ><A
1557 NAME="AEN17837"
1558 ></A
1559 >19.1. Here Strings</H1
1560 ><P
1561 ><A
1562 NAME="HERESTRINGSREF"
1563 ></A
1564 ></P
1565 ><A
1566 NAME="AEN17841"
1567 ></A
1568 ><BLOCKQUOTE
1569 CLASS="BLOCKQUOTE"
1570 ><P
1571 CLASS="LITERALLAYOUT"
1572 >A&nbsp;<I
1573 CLASS="FIRSTTERM"
1574 >here string</I
1575 >&nbsp;can&nbsp;be&nbsp;considered&nbsp;as&nbsp;a&nbsp;stripped-down&nbsp;form&nbsp;of&nbsp;a&nbsp;<I
1576 CLASS="FIRSTTERM"
1577 >here document</I
1578 >.<br>
1579 It&nbsp;consists&nbsp;of&nbsp;nothing&nbsp;more&nbsp;than&nbsp;<B
1580 CLASS="COMMAND"
1581 >COMMAND &#60;&#60;&#60; $WORD</B
1582 >,<br>
1583 where&nbsp;<TT
1584 CLASS="VARNAME"
1585 >$WORD</TT
1586 >&nbsp;is&nbsp;expanded&nbsp;and&nbsp;fed&nbsp;to&nbsp;the&nbsp;<TT
1587 CLASS="FILENAME"
1588 >stdin</TT
1589 >&nbsp;of&nbsp;<B
1590 CLASS="COMMAND"
1591 >COMMAND</B
1592 >.<br>
1593 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</P
1594 ></BLOCKQUOTE
1595 ><P
1596 >As a simple example, consider this alternative to the <A
1597 HREF="internal.html#ECHOGREPREF"
1598 >echo-grep</A
1599 > construction.</P
1600 ><P
1601 > <TABLE
1602 BORDER="0"
1603 BGCOLOR="#E0E0E0"
1604 WIDTH="100%"
1605 ><TR
1606 ><TD
1607 ><PRE
1608 CLASS="PROGRAMLISTING"
1609 > 1&nbsp;# Instead of:
1610 2&nbsp;if echo "$VAR" | grep -q txt # if [[ $VAR = *txt* ]]
1611 3&nbsp;# etc.
1612 4&nbsp;
1613 5&nbsp;# Try:
1614 6&nbsp;if grep -q "txt" &#60;&#60;&#60; "$VAR"
1615 7&nbsp;then # ^^^
1616 8&nbsp; echo "$VAR contains the substring sequence \"txt\""
1617 9&nbsp;fi
1618 10&nbsp;# Thank you, Sebastian Kaminski, for the suggestion.</PRE
1619 ></TD
1620 ></TR
1621 ></TABLE
1622 >
1623 </P
1624 ><P
1625 ><A
1626 NAME="HSREAD"
1627 ></A
1628 ></P
1629 ><P
1630 >Or, in combination with <A
1631 HREF="internal.html#READREF"
1632 >read</A
1633 >:</P
1634 ><P
1635 > <TABLE
1636 BORDER="0"
1637 BGCOLOR="#E0E0E0"
1638 WIDTH="100%"
1639 ><TR
1640 ><TD
1641 ><PRE
1642 CLASS="PROGRAMLISTING"
1643 > 1&nbsp;String="This is a string of words."
1644 2&nbsp;
1645 3&nbsp;read -r -a Words &#60;&#60;&#60; "$String"
1646 4&nbsp;# The -a option to "read"
1647 5&nbsp;#+ assigns the resulting values to successive members of an array.
1648 6&nbsp;
1649 7&nbsp;echo "First word in String is: ${Words[0]}" # This
1650 8&nbsp;echo "Second word in String is: ${Words[1]}" # is
1651 9&nbsp;echo "Third word in String is: ${Words[2]}" # a
1652 10&nbsp;echo "Fourth word in String is: ${Words[3]}" # string
1653 11&nbsp;echo "Fifth word in String is: ${Words[4]}" # of
1654 12&nbsp;echo "Sixth word in String is: ${Words[5]}" # words.
1655 13&nbsp;echo "Seventh word in String is: ${Words[6]}" # (null)
1656 14&nbsp; # Past end of $String.
1657 15&nbsp;
1658 16&nbsp;# Thank you, Francisco Lobo, for the suggestion.</PRE
1659 ></TD
1660 ></TR
1661 ></TABLE
1662 >
1663 </P
1664 ><P
1665 ><A
1666 NAME="HSLOOP"
1667 ></A
1668 >It is, of course, possible to feed
1669 the output of a <I
1670 CLASS="FIRSTTERM"
1671 >here string</I
1672 >
1673 into the <TT
1674 CLASS="FILENAME"
1675 >stdin</TT
1676 > of a <A
1677 HREF="loops.html#LOOPREF00"
1678 >loop</A
1679 >.</P
1680 ><P
1681 ><TABLE
1682 BORDER="0"
1683 BGCOLOR="#E0E0E0"
1684 WIDTH="100%"
1685 ><TR
1686 ><TD
1687 ><PRE
1688 CLASS="PROGRAMLISTING"
1689 > 1&nbsp;# As Seamus points out . . .
1690 2&nbsp;
1691 3&nbsp;ArrayVar=( element0 element1 element2 {A..D} )
1692 4&nbsp;
1693 5&nbsp;while read element ; do
1694 6&nbsp; echo "$element" 1&#62;&#38;2
1695 7&nbsp;done &#60;&#60;&#60; $(echo ${ArrayVar[*]})
1696 8&nbsp;
1697 9&nbsp;# element0 element1 element2 A B C D</PRE
1698 ></TD
1699 ></TR
1700 ></TABLE
1701 ></P
1702 ><P
1703 ><A
1704 NAME="HSPRE"
1705 ></A
1706 ></P
1707 ><DIV
1708 CLASS="EXAMPLE"
1709 ><HR><A
1710 NAME="PREPENDEX"
1711 ></A
1712 ><P
1713 ><B
1714 >Example 19-13. Prepending a line to a file</B
1715 ></P
1716 ><TABLE
1717 BORDER="0"
1718 BGCOLOR="#E0E0E0"
1719 WIDTH="100%"
1720 ><TR
1721 ><TD
1722 ><PRE
1723 CLASS="PROGRAMLISTING"
1724 > 1&nbsp;#!/bin/bash
1725 2&nbsp;# prepend.sh: Add text at beginning of file.
1726 3&nbsp;#
1727 4&nbsp;# Example contributed by Kenny Stauffer,
1728 5&nbsp;#+ and slightly modified by document author.
1729 6&nbsp;
1730 7&nbsp;
1731 8&nbsp;E_NOSUCHFILE=85
1732 9&nbsp;
1733 10&nbsp;read -p "File: " file # -p arg to 'read' displays prompt.
1734 11&nbsp;if [ ! -e "$file" ]
1735 12&nbsp;then # Bail out if no such file.
1736 13&nbsp; echo "File $file not found."
1737 14&nbsp; exit $E_NOSUCHFILE
1738 15&nbsp;fi
1739 16&nbsp;
1740 17&nbsp;read -p "Title: " title
1741 18&nbsp;cat - $file &#60;&#60;&#60;$title &#62; $file.new
1742 19&nbsp;
1743 20&nbsp;echo "Modified file is $file.new"
1744 21&nbsp;
1745 22&nbsp;exit # Ends script execution.
1746 23&nbsp;
1747 24&nbsp; from 'man bash':
1748 25&nbsp; Here Strings
1749 26&nbsp; A variant of here documents, the format is:
1750 27&nbsp;
1751 28&nbsp; &#60;&#60;&#60;word
1752 29&nbsp;
1753 30&nbsp; The word is expanded and supplied to the command on its standard input.
1754 31&nbsp;
1755 32&nbsp;
1756 33&nbsp; Of course, the following also works:
1757 34&nbsp; sed -e '1i\
1758 35&nbsp; Title: ' $file</PRE
1759 ></TD
1760 ></TR
1761 ></TABLE
1762 ><HR></DIV
1763 ><DIV
1764 CLASS="EXAMPLE"
1765 ><HR><A
1766 NAME="MAILBOXGREP"
1767 ></A
1768 ><P
1769 ><B
1770 >Example 19-14. Parsing a mailbox</B
1771 ></P
1772 ><TABLE
1773 BORDER="0"
1774 BGCOLOR="#E0E0E0"
1775 WIDTH="100%"
1776 ><TR
1777 ><TD
1778 ><PRE
1779 CLASS="PROGRAMLISTING"
1780 > 1&nbsp;#!/bin/bash
1781 2&nbsp;# Script by Francisco Lobo,
1782 3&nbsp;#+ and slightly modified and commented by ABS Guide author.
1783 4&nbsp;# Used in ABS Guide with permission. (Thank you!)
1784 5&nbsp;
1785 6&nbsp;# This script will not run under Bash versions -lt 3.0.
1786 7&nbsp;
1787 8&nbsp;
1788 9&nbsp;E_MISSING_ARG=87
1789 10&nbsp;if [ -z "$1" ]
1790 11&nbsp;then
1791 12&nbsp; echo "Usage: $0 mailbox-file"
1792 13&nbsp; exit $E_MISSING_ARG
1793 14&nbsp;fi
1794 15&nbsp;
1795 16&nbsp;mbox_grep() # Parse mailbox file.
1796 17&nbsp;{
1797 18&nbsp; declare -i body=0 match=0
1798 19&nbsp; declare -a date sender
1799 20&nbsp; declare mail header value
1800 21&nbsp;
1801 22&nbsp;
1802 23&nbsp; while IFS= read -r mail
1803 24&nbsp;# ^^^^ Reset $IFS.
1804 25&nbsp;# Otherwise "read" will strip leading &#38; trailing space from its input.
1805 26&nbsp;
1806 27&nbsp; do
1807 28&nbsp; if [[ $mail =~ ^From ]] # Match "From" field in message.
1808 29&nbsp; then
1809 30&nbsp; (( body = 0 )) # "Zero out" variables.
1810 31&nbsp; (( match = 0 ))
1811 32&nbsp; unset date
1812 33&nbsp;
1813 34&nbsp; elif (( body ))
1814 35&nbsp; then
1815 36&nbsp; (( match ))
1816 37&nbsp; # echo "$mail"
1817 38&nbsp; # Uncomment above line if you want entire body
1818 39&nbsp; #+ of message to display.
1819 40&nbsp;
1820 41&nbsp; elif [[ $mail ]]; then
1821 42&nbsp; IFS=: read -r header value &#60;&#60;&#60; "$mail"
1822 43&nbsp; # ^^^ "here string"
1823 44&nbsp;
1824 45&nbsp; case "$header" in
1825 46&nbsp; [Ff][Rr][Oo][Mm] ) [[ $value =~ "$2" ]] &#38;&#38; (( match++ )) ;;
1826 47&nbsp; # Match "From" line.
1827 48&nbsp; [Dd][Aa][Tt][Ee] ) read -r -a date &#60;&#60;&#60; "$value" ;;
1828 49&nbsp; # ^^^
1829 50&nbsp; # Match "Date" line.
1830 51&nbsp; [Rr][Ee][Cc][Ee][Ii][Vv][Ee][Dd] ) read -r -a sender &#60;&#60;&#60; "$value" ;;
1831 52&nbsp; # ^^^
1832 53&nbsp; # Match IP Address (may be spoofed).
1833 54&nbsp; esac
1834 55&nbsp;
1835 56&nbsp; else
1836 57&nbsp; (( body++ ))
1837 58&nbsp; (( match )) &#38;&#38;
1838 59&nbsp; echo "MESSAGE ${date:+of: ${date[*]} }"
1839 60&nbsp; # Entire $date array ^
1840 61&nbsp; echo "IP address of sender: ${sender[1]}"
1841 62&nbsp; # Second field of "Received" line ^
1842 63&nbsp;
1843 64&nbsp; fi
1844 65&nbsp;
1845 66&nbsp;
1846 67&nbsp; done &#60; "$1" # Redirect stdout of file into loop.
1847 68&nbsp;}
1848 69&nbsp;
1849 70&nbsp;
1850 71&nbsp;mbox_grep "$1" # Send mailbox file to function.
1851 72&nbsp;
1852 73&nbsp;exit $?
1853 74&nbsp;
1854 75&nbsp;# Exercises:
1855 76&nbsp;# ---------
1856 77&nbsp;# 1) Break the single function, above, into multiple functions,
1857 78&nbsp;#+ for the sake of readability.
1858 79&nbsp;# 2) Add additional parsing to the script, checking for various keywords.
1859 80&nbsp;
1860 81&nbsp;
1861 82&nbsp;
1862 83&nbsp;$ mailbox_grep.sh scam_mail
1863 84&nbsp; MESSAGE of Thu, 5 Jan 2006 08:00:56 -0500 (EST)
1864 85&nbsp; IP address of sender: 196.3.62.4</PRE
1865 ></TD
1866 ></TR
1867 ></TABLE
1868 ><HR></DIV
1869 ><P
1870 >Exercise: Find other uses for <I
1871 CLASS="FIRSTTERM"
1872 >here
1873 strings</I
1874 >, such as, for example, <A
1875 HREF="mathc.html#GOLDENRATIO"
1876 >feeding input to
1877 <I
1878 CLASS="FIRSTTERM"
1879 >dc</I
1880 ></A
1881 >.</P
1882 ></DIV
1883 ></DIV
1884 ><H3
1885 CLASS="FOOTNOTES"
1886 >Notes</H3
1887 ><TABLE
1888 BORDER="0"
1889 CLASS="FOOTNOTES"
1890 WIDTH="100%"
1891 ><TR
1892 ><TD
1893 ALIGN="LEFT"
1894 VALIGN="TOP"
1895 WIDTH="5%"
1896 ><A
1897 NAME="FTN.AEN17822"
1898 HREF="here-docs.html#AEN17822"
1899 >[1]</A
1900 ></TD
1901 ><TD
1902 ALIGN="LEFT"
1903 VALIGN="TOP"
1904 WIDTH="95%"
1905 ><P
1906 >Except, as Dennis Benzinger points out,
1907 if <A
1908 HREF="here-docs.html#LIMITSTRDASH"
1909 >using
1910 <B
1911 CLASS="COMMAND"
1912 >&#60;&#60;-</B
1913 > to suppress
1914 tabs</A
1915 >.</P
1916 ></TD
1917 ></TR
1918 ></TABLE
1919 ><DIV
1920 CLASS="NAVFOOTER"
1921 ><HR
1922 ALIGN="LEFT"
1923 WIDTH="100%"><TABLE
1924 SUMMARY="Footer navigation table"
1925 WIDTH="100%"
1926 BORDER="0"
1927 CELLPADDING="0"
1928 CELLSPACING="0"
1929 ><TR
1930 ><TD
1931 WIDTH="33%"
1932 ALIGN="left"
1933 VALIGN="top"
1934 ><A
1935 HREF="globbingref.html"
1936 ACCESSKEY="P"
1937 >Prev</A
1938 ></TD
1939 ><TD
1940 WIDTH="34%"
1941 ALIGN="center"
1942 VALIGN="top"
1943 ><A
1944 HREF="index.html"
1945 ACCESSKEY="H"
1946 >Home</A
1947 ></TD
1948 ><TD
1949 WIDTH="33%"
1950 ALIGN="right"
1951 VALIGN="top"
1952 ><A
1953 HREF="io-redirection.html"
1954 ACCESSKEY="N"
1955 >Next</A
1956 ></TD
1957 ></TR
1958 ><TR
1959 ><TD
1960 WIDTH="33%"
1961 ALIGN="left"
1962 VALIGN="top"
1963 >Globbing</TD
1964 ><TD
1965 WIDTH="34%"
1966 ALIGN="center"
1967 VALIGN="top"
1968 ><A
1969 HREF="part5.html"
1970 ACCESSKEY="U"
1971 >Up</A
1972 ></TD
1973 ><TD
1974 WIDTH="33%"
1975 ALIGN="right"
1976 VALIGN="top"
1977 >I/O Redirection</TD
1978 ></TR
1979 ></TABLE
1980 ></DIV
1981 ></BODY
1982 ></HTML
1983 >