| 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 | > </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 COMMAND <<InputComesFromHERE |
| 163 | 2 ... |
| 164 | 3 ... |
| 165 | 4 ... |
| 166 | 5 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 | ><<</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 < |
| 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 command #1 |
| 211 | 2 command #2 |
| 212 | 3 ...</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 interactive-program <<LimitString |
| 233 | 2 command #1 |
| 234 | 3 command #2 |
| 235 | 4 ... |
| 236 | 5 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 #!/bin/bash |
| 280 | 2 |
| 281 | 3 wall <<zzz23EndOfMessagezzz23 |
| 282 | 4 E-mail your noontime orders for pizza to the system administrator. |
| 283 | 5 (Add an extra dollar for anchovy or mushroom topping.) |
| 284 | 6 # Additional message text goes here. |
| 285 | 7 # Note: 'wall' prints comment lines. |
| 286 | 8 zzz23EndOfMessagezzz23 |
| 287 | 9 |
| 288 | 10 # Could have been done more efficiently by |
| 289 | 11 # wall <message-file |
| 290 | 12 # However, embedding the message template in a script |
| 291 | 13 #+ is a quick-and-dirty one-off solution. |
| 292 | 14 |
| 293 | 15 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 #!/bin/bash |
| 335 | 2 |
| 336 | 3 # Noninteractive use of 'vi' to edit a file. |
| 337 | 4 # Emulates 'sed'. |
| 338 | 5 |
| 339 | 6 E_BADARGS=85 |
| 340 | 7 |
| 341 | 8 if [ -z "$1" ] |
| 342 | 9 then |
| 343 | 10 echo "Usage: `basename $0` filename" |
| 344 | 11 exit $E_BADARGS |
| 345 | 12 fi |
| 346 | 13 |
| 347 | 14 TARGETFILE=$1 |
| 348 | 15 |
| 349 | 16 # Insert 2 lines in file, then save. |
| 350 | 17 #--------Begin here document-----------# |
| 351 | 18 vi $TARGETFILE <<x23LimitStringx23 |
| 352 | 19 i |
| 353 | 20 This is line 1 of the example file. |
| 354 | 21 This is line 2 of the example file. |
| 355 | 22 ^[ |
| 356 | 23 ZZ |
| 357 | 24 x23LimitStringx23 |
| 358 | 25 #----------End here document-----------# |
| 359 | 26 |
| 360 | 27 # Note that ^[ above is a literal escape |
| 361 | 28 #+ typed by Control-V <Esc>. |
| 362 | 29 |
| 363 | 30 # Bram Moolenaar points out that this may not work with 'vim' |
| 364 | 31 #+ because of possible problems with terminal interaction. |
| 365 | 32 |
| 366 | 33 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 #!/bin/bash |
| 406 | 2 # Replace all instances of "Smith" with "Jones" |
| 407 | 3 #+ in files with a ".txt" filename suffix. |
| 408 | 4 |
| 409 | 5 ORIGINAL=Smith |
| 410 | 6 REPLACEMENT=Jones |
| 411 | 7 |
| 412 | 8 for word in $(fgrep -l $ORIGINAL *.txt) |
| 413 | 9 do |
| 414 | 10 # ------------------------------------- |
| 415 | 11 ex $word <<EOF |
| 416 | 12 :%s/$ORIGINAL/$REPLACEMENT/g |
| 417 | 13 :wq |
| 418 | 14 EOF |
| 419 | 15 # :%s is the "ex" substitution command. |
| 420 | 16 # :wq is write-and-quit. |
| 421 | 17 # ------------------------------------- |
| 422 | 18 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 #!/bin/bash |
| 463 | 2 |
| 464 | 3 # 'echo' is fine for printing single line messages, |
| 465 | 4 #+ but somewhat problematic for for message blocks. |
| 466 | 5 # A 'cat' here document overcomes this limitation. |
| 467 | 6 |
| 468 | 7 cat <<End-of-message |
| 469 | 8 ------------------------------------- |
| 470 | 9 This is line 1 of the message. |
| 471 | 10 This is line 2 of the message. |
| 472 | 11 This is line 3 of the message. |
| 473 | 12 This is line 4 of the message. |
| 474 | 13 This is the last line of the message. |
| 475 | 14 ------------------------------------- |
| 476 | 15 End-of-message |
| 477 | 16 |
| 478 | 17 # Replacing line 7, above, with |
| 479 | 18 #+ cat > $Newfile <<End-of-message |
| 480 | 19 #+ ^^^^^^^^^^ |
| 481 | 20 #+ writes the output to the file $Newfile, rather than to stdout. |
| 482 | 21 |
| 483 | 22 exit 0 |
| 484 | 23 |
| 485 | 24 |
| 486 | 25 #-------------------------------------------- |
| 487 | 26 # Code below disabled, due to "exit 0" above. |
| 488 | 27 |
| 489 | 28 # S.C. points out that the following also works. |
| 490 | 29 echo "------------------------------------- |
| 491 | 30 This is line 1 of the message. |
| 492 | 31 This is line 2 of the message. |
| 493 | 32 This is line 3 of the message. |
| 494 | 33 This is line 4 of the message. |
| 495 | 34 This is the last line of the message. |
| 496 | 35 -------------------------------------" |
| 497 | 36 # 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 | ><<-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 #!/bin/bash |
| 538 | 2 # Same as previous example, but... |
| 539 | 3 |
| 540 | 4 # The - option to a here document <<- |
| 541 | 5 #+ suppresses leading tabs in the body of the document, |
| 542 | 6 #+ but *not* spaces. |
| 543 | 7 |
| 544 | 8 cat <<-ENDOFMESSAGE |
| 545 | 9 This is line 1 of the message. |
| 546 | 10 This is line 2 of the message. |
| 547 | 11 This is line 3 of the message. |
| 548 | 12 This is line 4 of the message. |
| 549 | 13 This is the last line of the message. |
| 550 | 14 ENDOFMESSAGE |
| 551 | 15 # The output of the script will be flush left. |
| 552 | 16 # Leading tab in each line will not show. |
| 553 | 17 |
| 554 | 18 # Above 5 lines of "message" prefaced by a tab, not spaces. |
| 555 | 19 # Spaces not affected by <<- . |
| 556 | 20 |
| 557 | 21 # Note that this option has no effect on *embedded* tabs. |
| 558 | 22 |
| 559 | 23 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 #!/bin/bash |
| 595 | 2 # Another 'cat' here document, using parameter substitution. |
| 596 | 3 |
| 597 | 4 # Try it with no command-line parameters, ./scriptname |
| 598 | 5 # Try it with one command-line parameter, ./scriptname Mortimer |
| 599 | 6 # Try it with one two-word quoted command-line parameter, |
| 600 | 7 # ./scriptname "Mortimer Jones" |
| 601 | 8 |
| 602 | 9 CMDLINEPARAM=1 # Expect at least command-line parameter. |
| 603 | 10 |
| 604 | 11 if [ $# -ge $CMDLINEPARAM ] |
| 605 | 12 then |
| 606 | 13 NAME=$1 # If more than one command-line param, |
| 607 | 14 #+ then just take the first. |
| 608 | 15 else |
| 609 | 16 NAME="John Doe" # Default, if no command-line parameter. |
| 610 | 17 fi |
| 611 | 18 |
| 612 | 19 RESPONDENT="the author of this fine script" |
| 613 | 20 |
| 614 | 21 |
| 615 | 22 cat <<Endofmessage |
| 616 | 23 |
| 617 | 24 Hello, there, $NAME. |
| 618 | 25 Greetings to you, $NAME, from $RESPONDENT. |
| 619 | 26 |
| 620 | 27 # This comment shows up in the output (why?). |
| 621 | 28 |
| 622 | 29 Endofmessage |
| 623 | 30 |
| 624 | 31 # Note that the blank lines show up in the output. |
| 625 | 32 # So does the comment. |
| 626 | 33 |
| 627 | 34 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 #!/bin/bash |
| 665 | 2 # upload.sh |
| 666 | 3 |
| 667 | 4 # Upload file pair (Filename.lsm, Filename.tar.gz) |
| 668 | 5 #+ to incoming directory at Sunsite/UNC (ibiblio.org). |
| 669 | 6 # Filename.tar.gz is the tarball itself. |
| 670 | 7 # Filename.lsm is the descriptor file. |
| 671 | 8 # Sunsite requires "lsm" file, otherwise will bounce contributions. |
| 672 | 9 |
| 673 | 10 |
| 674 | 11 E_ARGERROR=85 |
| 675 | 12 |
| 676 | 13 if [ -z "$1" ] |
| 677 | 14 then |
| 678 | 15 echo "Usage: `basename $0` Filename-to-upload" |
| 679 | 16 exit $E_ARGERROR |
| 680 | 17 fi |
| 681 | 18 |
| 682 | 19 |
| 683 | 20 Filename=`basename $1` # Strips pathname out of file name. |
| 684 | 21 |
| 685 | 22 Server="ibiblio.org" |
| 686 | 23 Directory="/incoming/Linux" |
| 687 | 24 # These need not be hard-coded into script, |
| 688 | 25 #+ but may instead be changed to command-line argument. |
| 689 | 26 |
| 690 | 27 Password="your.e-mail.address" # Change above to suit. |
| 691 | 28 |
| 692 | 29 ftp -n $Server <<End-Of-Session |
| 693 | 30 # -n option disables auto-logon |
| 694 | 31 |
| 695 | 32 user anonymous "$Password" # If this doesn't work, then try: |
| 696 | 33 # quote user anonymous "$Password" |
| 697 | 34 binary |
| 698 | 35 bell # Ring 'bell' after each file transfer. |
| 699 | 36 cd $Directory |
| 700 | 37 put "$Filename.lsm" |
| 701 | 38 put "$Filename.tar.gz" |
| 702 | 39 bye |
| 703 | 40 End-Of-Session |
| 704 | 41 |
| 705 | 42 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 #!/bin/bash |
| 762 | 2 # A 'cat' here-document, but with parameter substitution disabled. |
| 763 | 3 |
| 764 | 4 NAME="John Doe" |
| 765 | 5 RESPONDENT="the author of this fine script" |
| 766 | 6 |
| 767 | 7 cat <<'Endofmessage' |
| 768 | 8 |
| 769 | 9 Hello, there, $NAME. |
| 770 | 10 Greetings to you, $NAME, from $RESPONDENT. |
| 771 | 11 |
| 772 | 12 Endofmessage |
| 773 | 13 |
| 774 | 14 # No parameter substitution when the "limit string" is quoted or escaped. |
| 775 | 15 # Either of the following at the head of the here document would have |
| 776 | 16 #+ the same effect. |
| 777 | 17 # cat <<"Endofmessage" |
| 778 | 18 # cat <<\Endofmessage |
| 779 | 19 |
| 780 | 20 |
| 781 | 21 |
| 782 | 22 # And, likewise: |
| 783 | 23 |
| 784 | 24 cat <<"SpecialCharTest" |
| 785 | 25 |
| 786 | 26 Directory listing would follow |
| 787 | 27 if limit string were not quoted. |
| 788 | 28 `ls -l` |
| 789 | 29 |
| 790 | 30 Arithmetic expansion would take place |
| 791 | 31 if limit string were not quoted. |
| 792 | 32 $((5 + 3)) |
| 793 | 33 |
| 794 | 34 A a single backslash would echo |
| 795 | 35 if limit string were not quoted. |
| 796 | 36 \\ |
| 797 | 37 |
| 798 | 38 SpecialCharTest |
| 799 | 39 |
| 800 | 40 |
| 801 | 41 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 #!/bin/bash |
| 832 | 2 # generate-script.sh |
| 833 | 3 # Based on an idea by Albert Reiner. |
| 834 | 4 |
| 835 | 5 OUTFILE=generated.sh # Name of the file to generate. |
| 836 | 6 |
| 837 | 7 |
| 838 | 8 # ----------------------------------------------------------- |
| 839 | 9 # 'Here document containing the body of the generated script. |
| 840 | 10 ( |
| 841 | 11 cat <<'EOF' |
| 842 | 12 #!/bin/bash |
| 843 | 13 |
| 844 | 14 echo "This is a generated shell script." |
| 845 | 15 # Note that since we are inside a subshell, |
| 846 | 16 #+ we can't access variables in the "outside" script. |
| 847 | 17 |
| 848 | 18 echo "Generated file will be named: $OUTFILE" |
| 849 | 19 # Above line will not work as normally expected |
| 850 | 20 #+ because parameter expansion has been disabled. |
| 851 | 21 # Instead, the result is literal output. |
| 852 | 22 |
| 853 | 23 a=7 |
| 854 | 24 b=3 |
| 855 | 25 |
| 856 | 26 let "c = $a * $b" |
| 857 | 27 echo "c = $c" |
| 858 | 28 |
| 859 | 29 exit 0 |
| 860 | 30 EOF |
| 861 | 31 ) > $OUTFILE |
| 862 | 32 # ----------------------------------------------------------- |
| 863 | 33 |
| 864 | 34 # Quoting the 'limit string' prevents variable expansion |
| 865 | 35 #+ within the body of the above 'here document.' |
| 866 | 36 # This permits outputting literal strings in the output file. |
| 867 | 37 |
| 868 | 38 if [ -f "$OUTFILE" ] |
| 869 | 39 then |
| 870 | 40 chmod 755 $OUTFILE |
| 871 | 41 # Make the generated file executable. |
| 872 | 42 else |
| 873 | 43 echo "Problem in creating file: \"$OUTFILE\"" |
| 874 | 44 fi |
| 875 | 45 |
| 876 | 46 # This method also works for generating |
| 877 | 47 #+ C programs, Perl programs, Python programs, Makefiles, |
| 878 | 48 #+ and the like. |
| 879 | 49 |
| 880 | 50 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 variable=$(cat <<SETVAR |
| 905 | 2 This variable |
| 906 | 3 runs over multiple lines. |
| 907 | 4 SETVAR |
| 908 | 5 ) |
| 909 | 6 |
| 910 | 7 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 #!/bin/bash |
| 942 | 2 # here-function.sh |
| 943 | 3 |
| 944 | 4 GetPersonalData () |
| 945 | 5 { |
| 946 | 6 read firstname |
| 947 | 7 read lastname |
| 948 | 8 read address |
| 949 | 9 read city |
| 950 | 10 read state |
| 951 | 11 read zipcode |
| 952 | 12 } # This certainly appears to be an interactive function, but . . . |
| 953 | 13 |
| 954 | 14 |
| 955 | 15 # Supply input to the above function. |
| 956 | 16 GetPersonalData <<RECORD001 |
| 957 | 17 Bozo |
| 958 | 18 Bozeman |
| 959 | 19 2726 Nondescript Dr. |
| 960 | 20 Bozeman |
| 961 | 21 MT |
| 962 | 22 21226 |
| 963 | 23 RECORD001 |
| 964 | 24 |
| 965 | 25 |
| 966 | 26 echo |
| 967 | 27 echo "$firstname $lastname" |
| 968 | 28 echo "$address" |
| 969 | 29 echo "$city, $state $zipcode" |
| 970 | 30 echo |
| 971 | 31 |
| 972 | 32 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 #!/bin/bash |
| 1013 | 2 |
| 1014 | 3 : <<TESTVARIABLES |
| 1015 | 4 ${HOSTNAME?}${USER?}${MAIL?} # Print error message if one of the variables not set. |
| 1016 | 5 TESTVARIABLES |
| 1017 | 6 |
| 1018 | 7 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 #!/bin/bash |
| 1074 | 2 # commentblock.sh |
| 1075 | 3 |
| 1076 | 4 : <<COMMENTBLOCK |
| 1077 | 5 echo "This line will not echo." |
| 1078 | 6 This is a comment line missing the "#" prefix. |
| 1079 | 7 This is another comment line missing the "#" prefix. |
| 1080 | 8 |
| 1081 | 9 &*@!!++= |
| 1082 | 10 The above line will cause no error message, |
| 1083 | 11 because the Bash interpreter will ignore it. |
| 1084 | 12 COMMENTBLOCK |
| 1085 | 13 |
| 1086 | 14 echo "Exit value of above \"COMMENTBLOCK\" is $?." # 0 |
| 1087 | 15 # No error shown. |
| 1088 | 16 echo |
| 1089 | 17 |
| 1090 | 18 |
| 1091 | 19 # The above technique also comes in useful for commenting out |
| 1092 | 20 #+ a block of working code for debugging purposes. |
| 1093 | 21 # This saves having to put a "#" at the beginning of each line, |
| 1094 | 22 #+ then having to go back and delete each "#" later. |
| 1095 | 23 # Note that the use of of colon, above, is optional. |
| 1096 | 24 |
| 1097 | 25 echo "Just before commented-out code block." |
| 1098 | 26 # The lines of code between the double-dashed lines will not execute. |
| 1099 | 27 # =================================================================== |
| 1100 | 28 : <<DEBUGXXX |
| 1101 | 29 for file in * |
| 1102 | 30 do |
| 1103 | 31 cat "$file" |
| 1104 | 32 done |
| 1105 | 33 DEBUGXXX |
| 1106 | 34 # =================================================================== |
| 1107 | 35 echo "Just after commented-out code block." |
| 1108 | 36 |
| 1109 | 37 exit 0 |
| 1110 | 38 |
| 1111 | 39 |
| 1112 | 40 |
| 1113 | 41 ###################################################################### |
| 1114 | 42 # Note, however, that if a bracketed variable is contained within |
| 1115 | 43 #+ the commented-out code block, |
| 1116 | 44 #+ then this could cause problems. |
| 1117 | 45 # for example: |
| 1118 | 46 |
| 1119 | 47 |
| 1120 | 48 #/!/bin/bash |
| 1121 | 49 |
| 1122 | 50 : <<COMMENTBLOCK |
| 1123 | 51 echo "This line will not echo." |
| 1124 | 52 &*@!!++= |
| 1125 | 53 ${foo_bar_bazz?} |
| 1126 | 54 $(rm -rf /tmp/foobar/) |
| 1127 | 55 $(touch my_build_directory/cups/Makefile) |
| 1128 | 56 COMMENTBLOCK |
| 1129 | 57 |
| 1130 | 58 |
| 1131 | 59 $ sh commented-bad.sh |
| 1132 | 60 commented-bad.sh: line 3: foo_bar_bazz: parameter null or not set |
| 1133 | 61 |
| 1134 | 62 # The remedy for this is to strong-quote the 'COMMENTBLOCK' in line 49, above. |
| 1135 | 63 |
| 1136 | 64 : <<'COMMENTBLOCK' |
| 1137 | 65 |
| 1138 | 66 # 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 #!/bin/bash |
| 1194 | 2 # self-document.sh: self-documenting script |
| 1195 | 3 # Modification of "colm.sh". |
| 1196 | 4 |
| 1197 | 5 DOC_REQUEST=70 |
| 1198 | 6 |
| 1199 | 7 if [ "$1" = "-h" -o "$1" = "--help" ] # Request help. |
| 1200 | 8 then |
| 1201 | 9 echo; echo "Usage: $0 [directory-name]"; echo |
| 1202 | 10 sed --silent -e '/DOCUMENTATIONXX$/,/^DOCUMENTATIONXX$/p' "$0" | |
| 1203 | 11 sed -e '/DOCUMENTATIONXX$/d'; exit $DOC_REQUEST; fi |
| 1204 | 12 |
| 1205 | 13 |
| 1206 | 14 : <<DOCUMENTATIONXX |
| 1207 | 15 List the statistics of a specified directory in tabular format. |
| 1208 | 16 --------------------------------------------------------------- |
| 1209 | 17 The command-line parameter gives the directory to be listed. |
| 1210 | 18 If no directory specified or directory specified cannot be read, |
| 1211 | 19 then list the current working directory. |
| 1212 | 20 |
| 1213 | 21 DOCUMENTATIONXX |
| 1214 | 22 |
| 1215 | 23 if [ -z "$1" -o ! -r "$1" ] |
| 1216 | 24 then |
| 1217 | 25 directory=. |
| 1218 | 26 else |
| 1219 | 27 directory="$1" |
| 1220 | 28 fi |
| 1221 | 29 |
| 1222 | 30 echo "Listing of "$directory":"; echo |
| 1223 | 31 (printf "PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n" \ |
| 1224 | 32 ; ls -l "$directory" | sed 1d) | column -t |
| 1225 | 33 |
| 1226 | 34 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 DOC_REQUEST=70 |
| 1247 | 2 |
| 1248 | 3 if [ "$1" = "-h" -o "$1" = "--help" ] # Request help. |
| 1249 | 4 then # Use a "cat script" . . . |
| 1250 | 5 cat <<DOCUMENTATIONXX |
| 1251 | 6 List the statistics of a specified directory in tabular format. |
| 1252 | 7 --------------------------------------------------------------- |
| 1253 | 8 The command-line parameter gives the directory to be listed. |
| 1254 | 9 If no directory specified or directory specified cannot be read, |
| 1255 | 10 then list the current working directory. |
| 1256 | 11 |
| 1257 | 12 DOCUMENTATIONXX |
| 1258 | 13 exit $DOC_REQUEST |
| 1259 | 14 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' << EOF</B |
| 1324 | ></TT |
| 1325 | > |
| 1326 | <TT |
| 1327 | CLASS="PROMPT" |
| 1328 | >> </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 #!/bin/bash |
| 1441 | 2 |
| 1442 | 3 echo "----------------------------------------------------------------------" |
| 1443 | 4 |
| 1444 | 5 cat <<LimitString |
| 1445 | 6 echo "This is line 1 of the message inside the here document." |
| 1446 | 7 echo "This is line 2 of the message inside the here document." |
| 1447 | 8 echo "This is the final line of the message inside the here document." |
| 1448 | 9 LimitString |
| 1449 | 10 #^^^^Indented limit string. Error! This script will not behave as expected. |
| 1450 | 11 |
| 1451 | 12 echo "----------------------------------------------------------------------" |
| 1452 | 13 |
| 1453 | 14 # These comments are outside the 'here document', |
| 1454 | 15 #+ and should not echo. |
| 1455 | 16 |
| 1456 | 17 echo "Outside the here document." |
| 1457 | 18 |
| 1458 | 19 exit 0 |
| 1459 | 20 |
| 1460 | 21 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 # This works. |
| 1508 | 2 cat <<! |
| 1509 | 3 Hello! |
| 1510 | 4 ! Three more exclamations !!! |
| 1511 | 5 ! |
| 1512 | 6 |
| 1513 | 7 |
| 1514 | 8 # But . . . |
| 1515 | 9 cat <<! |
| 1516 | 10 Hello! |
| 1517 | 11 Single exclamation point follows! |
| 1518 | 12 ! |
| 1519 | 13 ! |
| 1520 | 14 # Crashes with an error message. |
| 1521 | 15 |
| 1522 | 16 |
| 1523 | 17 # However, the following will work. |
| 1524 | 18 cat <<EOF |
| 1525 | 19 Hello! |
| 1526 | 20 Single exclamation point follows! |
| 1527 | 21 ! |
| 1528 | 22 EOF |
| 1529 | 23 # 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 <I |
| 1573 | CLASS="FIRSTTERM" |
| 1574 | >here string</I |
| 1575 | > can be considered as a stripped-down form of a <I |
| 1576 | CLASS="FIRSTTERM" |
| 1577 | >here document</I |
| 1578 | >.<br> |
| 1579 | It consists of nothing more than <B |
| 1580 | CLASS="COMMAND" |
| 1581 | >COMMAND <<< $WORD</B |
| 1582 | >,<br> |
| 1583 | where <TT |
| 1584 | CLASS="VARNAME" |
| 1585 | >$WORD</TT |
| 1586 | > is expanded and fed to the <TT |
| 1587 | CLASS="FILENAME" |
| 1588 | >stdin</TT |
| 1589 | > of <B |
| 1590 | CLASS="COMMAND" |
| 1591 | >COMMAND</B |
| 1592 | >.<br> |
| 1593 | </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 # Instead of: |
| 1610 | 2 if echo "$VAR" | grep -q txt # if [[ $VAR = *txt* ]] |
| 1611 | 3 # etc. |
| 1612 | 4 |
| 1613 | 5 # Try: |
| 1614 | 6 if grep -q "txt" <<< "$VAR" |
| 1615 | 7 then # ^^^ |
| 1616 | 8 echo "$VAR contains the substring sequence \"txt\"" |
| 1617 | 9 fi |
| 1618 | 10 # 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 String="This is a string of words." |
| 1644 | 2 |
| 1645 | 3 read -r -a Words <<< "$String" |
| 1646 | 4 # The -a option to "read" |
| 1647 | 5 #+ assigns the resulting values to successive members of an array. |
| 1648 | 6 |
| 1649 | 7 echo "First word in String is: ${Words[0]}" # This |
| 1650 | 8 echo "Second word in String is: ${Words[1]}" # is |
| 1651 | 9 echo "Third word in String is: ${Words[2]}" # a |
| 1652 | 10 echo "Fourth word in String is: ${Words[3]}" # string |
| 1653 | 11 echo "Fifth word in String is: ${Words[4]}" # of |
| 1654 | 12 echo "Sixth word in String is: ${Words[5]}" # words. |
| 1655 | 13 echo "Seventh word in String is: ${Words[6]}" # (null) |
| 1656 | 14 # Past end of $String. |
| 1657 | 15 |
| 1658 | 16 # 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 # As Seamus points out . . . |
| 1690 | 2 |
| 1691 | 3 ArrayVar=( element0 element1 element2 {A..D} ) |
| 1692 | 4 |
| 1693 | 5 while read element ; do |
| 1694 | 6 echo "$element" 1>&2 |
| 1695 | 7 done <<< $(echo ${ArrayVar[*]}) |
| 1696 | 8 |
| 1697 | 9 # 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 #!/bin/bash |
| 1725 | 2 # prepend.sh: Add text at beginning of file. |
| 1726 | 3 # |
| 1727 | 4 # Example contributed by Kenny Stauffer, |
| 1728 | 5 #+ and slightly modified by document author. |
| 1729 | 6 |
| 1730 | 7 |
| 1731 | 8 E_NOSUCHFILE=85 |
| 1732 | 9 |
| 1733 | 10 read -p "File: " file # -p arg to 'read' displays prompt. |
| 1734 | 11 if [ ! -e "$file" ] |
| 1735 | 12 then # Bail out if no such file. |
| 1736 | 13 echo "File $file not found." |
| 1737 | 14 exit $E_NOSUCHFILE |
| 1738 | 15 fi |
| 1739 | 16 |
| 1740 | 17 read -p "Title: " title |
| 1741 | 18 cat - $file <<<$title > $file.new |
| 1742 | 19 |
| 1743 | 20 echo "Modified file is $file.new" |
| 1744 | 21 |
| 1745 | 22 exit # Ends script execution. |
| 1746 | 23 |
| 1747 | 24 from 'man bash': |
| 1748 | 25 Here Strings |
| 1749 | 26 A variant of here documents, the format is: |
| 1750 | 27 |
| 1751 | 28 <<<word |
| 1752 | 29 |
| 1753 | 30 The word is expanded and supplied to the command on its standard input. |
| 1754 | 31 |
| 1755 | 32 |
| 1756 | 33 Of course, the following also works: |
| 1757 | 34 sed -e '1i\ |
| 1758 | 35 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 #!/bin/bash |
| 1781 | 2 # Script by Francisco Lobo, |
| 1782 | 3 #+ and slightly modified and commented by ABS Guide author. |
| 1783 | 4 # Used in ABS Guide with permission. (Thank you!) |
| 1784 | 5 |
| 1785 | 6 # This script will not run under Bash versions -lt 3.0. |
| 1786 | 7 |
| 1787 | 8 |
| 1788 | 9 E_MISSING_ARG=87 |
| 1789 | 10 if [ -z "$1" ] |
| 1790 | 11 then |
| 1791 | 12 echo "Usage: $0 mailbox-file" |
| 1792 | 13 exit $E_MISSING_ARG |
| 1793 | 14 fi |
| 1794 | 15 |
| 1795 | 16 mbox_grep() # Parse mailbox file. |
| 1796 | 17 { |
| 1797 | 18 declare -i body=0 match=0 |
| 1798 | 19 declare -a date sender |
| 1799 | 20 declare mail header value |
| 1800 | 21 |
| 1801 | 22 |
| 1802 | 23 while IFS= read -r mail |
| 1803 | 24 # ^^^^ Reset $IFS. |
| 1804 | 25 # Otherwise "read" will strip leading & trailing space from its input. |
| 1805 | 26 |
| 1806 | 27 do |
| 1807 | 28 if [[ $mail =~ ^From ]] # Match "From" field in message. |
| 1808 | 29 then |
| 1809 | 30 (( body = 0 )) # "Zero out" variables. |
| 1810 | 31 (( match = 0 )) |
| 1811 | 32 unset date |
| 1812 | 33 |
| 1813 | 34 elif (( body )) |
| 1814 | 35 then |
| 1815 | 36 (( match )) |
| 1816 | 37 # echo "$mail" |
| 1817 | 38 # Uncomment above line if you want entire body |
| 1818 | 39 #+ of message to display. |
| 1819 | 40 |
| 1820 | 41 elif [[ $mail ]]; then |
| 1821 | 42 IFS=: read -r header value <<< "$mail" |
| 1822 | 43 # ^^^ "here string" |
| 1823 | 44 |
| 1824 | 45 case "$header" in |
| 1825 | 46 [Ff][Rr][Oo][Mm] ) [[ $value =~ "$2" ]] && (( match++ )) ;; |
| 1826 | 47 # Match "From" line. |
| 1827 | 48 [Dd][Aa][Tt][Ee] ) read -r -a date <<< "$value" ;; |
| 1828 | 49 # ^^^ |
| 1829 | 50 # Match "Date" line. |
| 1830 | 51 [Rr][Ee][Cc][Ee][Ii][Vv][Ee][Dd] ) read -r -a sender <<< "$value" ;; |
| 1831 | 52 # ^^^ |
| 1832 | 53 # Match IP Address (may be spoofed). |
| 1833 | 54 esac |
| 1834 | 55 |
| 1835 | 56 else |
| 1836 | 57 (( body++ )) |
| 1837 | 58 (( match )) && |
| 1838 | 59 echo "MESSAGE ${date:+of: ${date[*]} }" |
| 1839 | 60 # Entire $date array ^ |
| 1840 | 61 echo "IP address of sender: ${sender[1]}" |
| 1841 | 62 # Second field of "Received" line ^ |
| 1842 | 63 |
| 1843 | 64 fi |
| 1844 | 65 |
| 1845 | 66 |
| 1846 | 67 done < "$1" # Redirect stdout of file into loop. |
| 1847 | 68 } |
| 1848 | 69 |
| 1849 | 70 |
| 1850 | 71 mbox_grep "$1" # Send mailbox file to function. |
| 1851 | 72 |
| 1852 | 73 exit $? |
| 1853 | 74 |
| 1854 | 75 # Exercises: |
| 1855 | 76 # --------- |
| 1856 | 77 # 1) Break the single function, above, into multiple functions, |
| 1857 | 78 #+ for the sake of readability. |
| 1858 | 79 # 2) Add additional parsing to the script, checking for various keywords. |
| 1859 | 80 |
| 1860 | 81 |
| 1861 | 82 |
| 1862 | 83 $ mailbox_grep.sh scam_mail |
| 1863 | 84 MESSAGE of Thu, 5 Jan 2006 08:00:56 -0500 (EST) |
| 1864 | 85 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 | ><<-</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 | > |