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