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 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Writing Scripts</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="Exercises"
HREF="exercises.html"><LINK
REL="PREVIOUS"
TITLE="Exercises"
HREF="exercises.html"><LINK
REL="NEXT"
TITLE="Revision History"
HREF="revisionhistory.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="SECT1"
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="exercises.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Appendix O. Exercises</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="revisionhistory.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="WRITINGSCRIPTS"
></A
>O.2. Writing Scripts</H1
><P
><A
NAME="WRITINGSCRIPTS1"
></A
></P
><P
>Write a script to carry out each of the following tasks.</P
><DIV
CLASS="VARIABLELIST"
><P
><B
><A
NAME="EXEASY1"
></A
>EASY</B
></P
><DL
><DT
><B
CLASS="COMMAND"
>Self-reproducing Script</B
></DT
><DD
><P
>Write a script that backs itself up, that is, copies
itself to a file named <TT
CLASS="FILENAME"
>backup.sh</TT
>.</P
><P
>Hint: Use the <A
HREF="external.html#CATREF"
>cat</A
> command
and the appropriate <A
HREF="othertypesv.html#SCRNAMEPARAM"
>positional
parameter</A
>.</P
></DD
><DT
><B
CLASS="COMMAND"
>Home Directory Listing</B
></DT
><DD
><P
>Perform a recursive directory listing on the user's home
directory and save the information to a file. Compress
the file, have the script prompt the user to insert
a USB flash drive, then press <B
CLASS="KEYCAP"
>ENTER</B
>.
Finally, save the file to the flash drive after making
certain the flash drive has properly mounted by parsing
the output of <A
HREF="system.html#DFREF"
>df</A
>. Note that
the flash drive must be <I
CLASS="FIRSTTERM"
>unmounted</I
>
before it is removed.</P
></DD
><DT
><B
CLASS="COMMAND"
>Converting <A
HREF="loops.html#FORLOOPREF1"
>for</A
>
loops to <A
HREF="loops.html#WHILELOOPREF"
>while</A
> and <A
HREF="loops.html#UNTILLOOPREF"
>until</A
> loops</B
></DT
><DD
><P
>Convert the <I
CLASS="FIRSTTERM"
>for loops</I
> in <A
HREF="loops.html#EX22"
>Example 11-1</A
> to <I
CLASS="FIRSTTERM"
>while
loops</I
>. Hint: store the data in an <A
HREF="arrays.html#ARRAYREF"
>array</A
> and step through the array
elements.</P
><P
>Having already done the <SPAN
CLASS="QUOTE"
>"heavy lifting,"</SPAN
>
now convert the loops in the example to <I
CLASS="FIRSTTERM"
> until
loops</I
>.</P
></DD
><DT
><B
CLASS="COMMAND"
>Changing the line spacing of a text file</B
></DT
><DD
><P
>Write a script that reads each line of a target file, then
writes the line back to <TT
CLASS="FILENAME"
>stdout</TT
>, but with
an extra blank line following. This has the effect of
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>double-spacing</I
></SPAN
> the file.</P
><P
>Include all necessary code to check whether the script
gets the necessary command-line argument (a filename),
and whether the specified file exists.</P
><P
>When the script runs correctly, modify it to
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>triple-space</I
></SPAN
> the target file.</P
><P
>Finally, write a script to remove all blank lines from
the target file, <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>single-spacing</I
></SPAN
> it.</P
></DD
><DT
><B
CLASS="COMMAND"
>Backwards Listing</B
></DT
><DD
><P
>Write a script that echoes itself to
<TT
CLASS="FILENAME"
>stdout</TT
>, but
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>backwards</I
></SPAN
>.</P
></DD
><DT
><B
CLASS="COMMAND"
>Automatically Decompressing Files</B
></DT
><DD
><P
>Given a list of filenames as input, this script
queries each target file (parsing the output of the
<A
HREF="filearchiv.html#FILEREF"
>file</A
> command) for
the type of compression used on it. Then the script
automatically invokes the appropriate decompression command
(<B
CLASS="COMMAND"
>gunzip</B
>, <B
CLASS="COMMAND"
>bunzip2</B
>,
<B
CLASS="COMMAND"
>unzip</B
>, <B
CLASS="COMMAND"
>uncompress</B
>,
or whatever). If a target file is not compressed, the
script emits a warning message, but takes no other action
on that particular file.</P
></DD
><DT
><B
CLASS="COMMAND"
>Unique System ID</B
></DT
><DD
><P
>Generate a <SPAN
CLASS="QUOTE"
>"unique"</SPAN
> 6-digit hexadecimal
identifier for your computer. Do <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>not</I
></SPAN
>
use the flawed <A
HREF="system.html#HOSTIDREF"
>hostid</A
>
command. Hint: <B
CLASS="COMMAND"
><A
HREF="filearchiv.html#MD5SUMREF"
>md5sum</A
>
<A
HREF="files.html#DATAFILESREF1"
><TT
CLASS="FILENAME"
>/etc/passwd</TT
></A
></B
>,
then select the first 6 digits of output.</P
></DD
><DT
><B
CLASS="COMMAND"
>Backup</B
></DT
><DD
><P
>Archive as a <SPAN
CLASS="QUOTE"
>"tarball"</SPAN
>
(<TT
CLASS="FILENAME"
>*.tar.gz</TT
> file) all the files
in your home directory tree
(<TT
CLASS="FILENAME"
>/home/your-name</TT
>) that have
been modified in the last 24 hours. Hint: use <A
HREF="moreadv.html#FINDREF"
>find</A
>.</P
><P
>Optional: you may use this as the basis of a
<I
CLASS="FIRSTTERM"
>backup</I
> script.</P
></DD
><DT
><B
CLASS="COMMAND"
>Checking whether a process is still running</B
></DT
><DD
><P
>Given a <A
HREF="special-chars.html#PROCESSIDREF"
>process ID</A
>
(<I
CLASS="FIRSTTERM"
>PID</I
>) as an argument, this script
will check, at user-specified intervals, whether
the given process is still running. You may use
the <A
HREF="system.html#PPSSREF"
>ps</A
> and <A
HREF="timedate.html#SLEEPREF"
>sleep</A
> commands.</P
></DD
><DT
><B
CLASS="COMMAND"
>Primes</B
></DT
><DD
><P
>Print (to <TT
CLASS="FILENAME"
>stdout</TT
>) all
prime numbers between 60000 and 63000. The output
should be nicely formatted in columns (hint:
use <A
HREF="internal.html#PRINTFREF"
>printf</A
>).</P
></DD
><DT
><B
CLASS="COMMAND"
>Lottery Numbers</B
></DT
><DD
><P
>One type of lottery involves picking five
different numbers, in the range of 1 - 50. Write a
script that generates five pseudorandom numbers in this
range, <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>with no duplicates</I
></SPAN
>. The
script will give the option of echoing the numbers to
<TT
CLASS="FILENAME"
>stdout</TT
> or saving them to a file,
along with the date and time the particular number set
was generated. (If your script consistently generates
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>winning</I
></SPAN
> lottery numbers, then you
can retire on the proceeds and leave shell scripting to
those of us who have to work for a living.)</P
></DD
></DL
></DIV
><DIV
CLASS="VARIABLELIST"
><P
><B
><A
NAME="EXMEDIUM1"
></A
>INTERMEDIATE</B
></P
><DL
><DT
><B
CLASS="COMMAND"
>Integer or String</B
></DT
><DD
><P
>Write a script <A
HREF="functions.html#FUNCTIONREF"
>function</A
>
that determines if an argument passed to it is an integer
or a string. The function will return TRUE (0) if
passed an integer, and FALSE (1) if passed a string.</P
><P
>Hint: What does the following expression return
when <TT
CLASS="VARNAME"
>$1</TT
> is <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>not</I
></SPAN
>
an integer?</P
><P
><TT
CLASS="VARNAME"
>expr $1 + 0</TT
></P
></DD
><DT
><B
CLASS="COMMAND"
><A
HREF="special-chars.html#ASCIIDEF"
>ASCII</A
>
to Integer</B
></DT
><DD
><P
>The <I
CLASS="FIRSTTERM"
>atoi</I
> function in
<B
CLASS="COMMAND"
>C</B
> converts a string character to
an integer. Write a shell script function that performs
the same operation. Likewise, write a shell script function
that does the inverse, mirroring the <B
CLASS="COMMAND"
>C</B
>
<I
CLASS="FIRSTTERM"
>itoa</I
> function which converts an
integer into an ASCII character.</P
></DD
><DT
><B
CLASS="COMMAND"
>Managing Disk Space</B
></DT
><DD
><P
>List, one at a time, all files larger than 100K in
the <TT
CLASS="FILENAME"
>/home/username</TT
>
directory tree. Give the user the option to delete or
compress the file, then proceed to show the next one. Write
to a logfile the names of all deleted files and the
deletion times.</P
></DD
><DT
><B
CLASS="COMMAND"
>Banner</B
></DT
><DD
><P
>Simulate the functionality of the deprecated <A
HREF="extmisc.html#BANNERREF"
>banner</A
> command in a script.</P
></DD
><DT
><B
CLASS="COMMAND"
>Removing Inactive Accounts</B
></DT
><DD
><P
>Inactive accounts on a network server waste disk space and may
become a security risk. Write an administrative script
(to be invoked by <I
CLASS="FIRSTTERM"
>root</I
> or the <A
HREF="system.html#CRONREF"
>cron daemon</A
>) that checks
for and deletes user accounts that have not been accessed
within the last 90 days.</P
></DD
><DT
><B
CLASS="COMMAND"
>Enforcing Disk Quotas</B
></DT
><DD
><P
>Write a script for a multi-user system that checks users'
disk usage. If a user surpasses a preset limit
(500 MB, for example) in her <TT
CLASS="FILENAME"
>/home/username</TT
>
directory, then the script automatically sends her a
<SPAN
CLASS="QUOTE"
>"pigout"</SPAN
> warning e-mail.</P
><P
>The
script will use the <A
HREF="system.html#DUREF"
>du</A
>
and <A
HREF="communications.html#COMMMAIL1"
>mail</A
> commands. As
an option, it will allow setting and enforcing quotas
using the <A
HREF="system.html#QUOTAREF"
>quota</A
> and <A
HREF="system.html#SETQUOTAREF"
>setquota</A
> commands.</P
></DD
><DT
><B
CLASS="COMMAND"
>Logged in User Information</B
></DT
><DD
><P
>For all logged in users, show their real names and the time
and date of their last login.</P
><P
>Hint: use <A
HREF="system.html#WHOREF"
>who</A
>,
<A
HREF="system.html#LASTLOGREF"
>lastlog</A
>,
and parse <A
HREF="files.html#DATAFILESREF1"
><TT
CLASS="FILENAME"
>/etc/passwd</TT
></A
>.</P
></DD
><DT
><B
CLASS="COMMAND"
>Safe Delete</B
></DT
><DD
><P
>Implement, as a script, a <SPAN
CLASS="QUOTE"
>"safe"</SPAN
> delete
command, <TT
CLASS="FILENAME"
>sdel.sh</TT
>. Filenames passed as
command-line arguments to this script are not deleted,
but instead <A
HREF="filearchiv.html#GZIPREF"
>gzipped</A
>
if not already compressed (use <A
HREF="filearchiv.html#FILEREF"
>file</A
> to check), then moved
to a <TT
CLASS="FILENAME"
>~/TRASH</TT
>
directory. Upon invocation, the script checks the <TT
CLASS="FILENAME"
>~/TRASH</TT
> directory for files
older than 48 hours and <A
HREF="external.html#RMREF"
>permanently
deletes</A
> them. (An better alternative might be to
have a second script handle this, periodically invoked
by the <A
HREF="system.html#CRONREF"
>cron daemon</A
>.)</P
><P
><SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>Extra credit:</I
></SPAN
> Write the script
so it can handle files and directories <A
HREF="external.html#RMRECURS"
>recursively</A
>. This would give it
the capability of <SPAN
CLASS="QUOTE"
>"safely deleting"</SPAN
> entire
directory structures.</P
></DD
><DT
><B
CLASS="COMMAND"
>Making Change</B
></DT
><DD
><P
>What is the most efficient way to make change for $1.68,
using only coins in common circulations (up to 25c)? It's
6 quarters, 1 dime, a nickel, and three cents.</P
><P
>Given any arbitrary command-line input in dollars and
cents ($*.??), calculate the change, using the minimum
number of coins. If your home country is not the United
States, you may use your local currency units instead. The
script will need to parse the command-line input, then
change it to multiples of the smallest monetary unit (cents
or whatever). Hint: look at <A
HREF="functions.html#EX61"
>Example 24-8</A
>.</P
></DD
><DT
><B
CLASS="COMMAND"
>Quadratic Equations</B
></DT
><DD
><P
>Solve a <I
CLASS="FIRSTTERM"
>quadratic</I
> equation of the form
<TT
CLASS="PARAMETER"
><I
>Ax^2 + Bx + C = 0</I
></TT
>. Have a script take
as arguments the coefficients, <TT
CLASS="USERINPUT"
><B
>A</B
></TT
>,
<TT
CLASS="USERINPUT"
><B
>B</B
></TT
>, and <TT
CLASS="USERINPUT"
><B
>C</B
></TT
>,
and return the solutions to five decimal places.</P
><P
>Hint: pipe the coefficients to <A
HREF="mathc.html#BCREF"
>bc</A
>, using the well-known formula,
<TT
CLASS="PARAMETER"
><I
>x = ( -B +/- sqrt( B^2 - 4AC ) ) / 2A</I
></TT
>.</P
></DD
><DT
><B
CLASS="COMMAND"
>Table of Logarithms</B
></DT
><DD
><P
>Using the <A
HREF="mathc.html#BCREF"
>bc</A
> and <A
HREF="internal.html#PRINTFREF"
>printf</A
> commands, print out a
nicely-formatted table of eight-place natural logarithms
in the interval between 0.00 and 100.00, in steps of
.01.</P
><P
>Hint: <I
CLASS="FIRSTTERM"
>bc</I
> requires the
<TT
CLASS="OPTION"
>-l</TT
> option to load the math library.</P
></DD
><DT
><B
CLASS="COMMAND"
>Unicode Table</B
></DT
><DD
><P
>Using <A
HREF="asciitable.html#ASCIISH"
>Example T-1</A
> as a template,
write a script that prints to a file a complete
<A
HREF="bashver4.html#UNICODEREF"
>Unicode</A
> table.</P
><P
>Hint: Use the <TT
CLASS="OPTION"
>-e</TT
> option to
<A
HREF="internal.html#ECHOREF"
>echo</A
>:
<B
CLASS="COMMAND"
>echo -e '\uXXXX'</B
>, where
<TT
CLASS="REPLACEABLE"
><I
>XXXX</I
></TT
>
is the Unicode numerical character designation.
This requires <A
HREF="bashver4.html#BASH42"
>version 4.2</A
>
or later of Bash.</P
></DD
><DT
><B
CLASS="COMMAND"
>Sum of Matching Numbers</B
></DT
><DD
><P
>Find the sum of all five-digit numbers (in the range
10000 - 99999) containing <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>exactly two</I
></SPAN
>
out of the following set of digits: { 4, 5, 6 }. These may
repeat within the same number, and if so, they count once
for each occurrence.</P
><P
>Some examples of <I
CLASS="FIRSTTERM"
>matching numbers</I
> are
42057, 74638, and 89515.</P
></DD
><DT
><B
CLASS="COMMAND"
>Lucky Numbers</B
></DT
><DD
><P
>A <I
CLASS="FIRSTTERM"
>lucky number</I
> is one whose
individual digits add up to 7, in successive additions. For
example, 62431 is a <I
CLASS="FIRSTTERM"
>lucky number</I
>
(6 + 2 + 4 + 3 + 1 = 16, 1 + 6 = 7). Find all the
<I
CLASS="FIRSTTERM"
>lucky numbers</I
> between 1000 and
10000.</P
></DD
><DT
><B
CLASS="COMMAND"
>Craps</B
></DT
><DD
><P
>Borrowing the ASCII graphics from <A
HREF="contributed-scripts.html#PETALS"
>Example A-40</A
>,
write a script that plays the well-known gambling game of
<I
CLASS="FIRSTTERM"
>craps</I
>. The script will accept bets
from one or more players, roll the dice, and keep track of
wins and losses, as well as of each player's bankroll.</P
></DD
><DT
><B
CLASS="COMMAND"
>Tic-tac-toe</B
></DT
><DD
><P
>Write a script that plays the child's game of
<I
CLASS="FIRSTTERM"
>tic-tac-toe</I
> against a human
player. The script will let the human choose whether
to take the first move. The script will follow
an optimal strategy, and therefore never lose. To simplify
matters, you may use ASCII graphics:</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 o | x |
2 ----------
3 | x |
4 ----------
5 | o |
6
7 Your move, human (row, column)?</PRE
></TD
></TR
></TABLE
></P
></DD
><DT
><B
CLASS="COMMAND"
>Alphabetizing a String</B
></DT
><DD
><P
>Alphabetize (in ASCII order) an arbitrary string
read from the command-line.</P
></DD
><DT
><B
CLASS="COMMAND"
>Parsing</B
></DT
><DD
><P
>Parse <A
HREF="files.html#DATAFILESREF1"
><TT
CLASS="FILENAME"
>/etc/passwd</TT
></A
>,
and output its contents in nice, easy-to-read tabular
form.</P
></DD
><DT
><B
CLASS="COMMAND"
>Logging Logins</B
></DT
><DD
><P
>Parse <TT
CLASS="FILENAME"
>/var/log/messages</TT
> to
produce a nicely formatted file of user logins and login
times. The script may need to run as
<I
CLASS="FIRSTTERM"
>root</I
>. (Hint: Search for the string
<SPAN
CLASS="QUOTE"
>"LOGIN."</SPAN
>)</P
></DD
><DT
><B
CLASS="COMMAND"
>Pretty-Printing a Data File</B
></DT
><DD
><P
>Certain database and spreadsheet packages use
save-files with the fields separated by commas, commonly
referred to as <I
CLASS="FIRSTTERM"
>comma-separated values</I
>
or CSVs. Other applications often need to parse these
files.</P
><P
>Given a data file with comma-separated
<A
HREF="special-chars.html#FIELDREF"
>fields</A
>, of the form:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 Jones,Bill,235 S. Williams St.,Denver,CO,80221,(303) 244-7989
2 Smith,Tom,404 Polk Ave.,Los Angeles,CA,90003,(213) 879-5612
3 ...</PRE
></TD
></TR
></TABLE
>
Reformat the data and print it out to
<TT
CLASS="FILENAME"
>stdout</TT
> in labeled, evenly-spaced columns.</P
></DD
><DT
><B
CLASS="COMMAND"
>Justification</B
></DT
><DD
><P
>Given ASCII text input either from
<TT
CLASS="FILENAME"
>stdin</TT
> or a file, adjust
the word spacing to right-justify each line to a
user-specified line-width, then send the output to
<TT
CLASS="FILENAME"
>stdout</TT
>.</P
></DD
><DT
><B
CLASS="COMMAND"
>Mailing List</B
></DT
><DD
><P
>Using the <A
HREF="communications.html#COMMMAIL1"
>mail</A
> command,
write a script that manages a simple mailing list. The
script automatically e-mails the monthly company newsletter,
read from a specified text file, and sends it to all the
addresses on the mailing list, which the script reads from
another specified file.</P
></DD
><DT
><B
CLASS="COMMAND"
>Generating Passwords</B
></DT
><DD
><P
>Generate pseudorandom 8-character passwords, using
characters in the ranges [0-9], [A-Z], [a-z]. Each password
must contain at least two digits.</P
></DD
><DT
><B
CLASS="COMMAND"
>Monitoring a User</B
></DT
><DD
><P
>You suspect that one particular user on the network
has been abusing her privileges and possibly attempting to
hack the system. Write a script to automatically monitor
and log her activities when she's signed on. The log file
will save entries for the previous week, and delete those
entries more than seven days old.</P
><P
>You may use <A
HREF="system.html#LASTREF"
>last</A
>,
<A
HREF="system.html#LASTLOGREF"
>lastlog</A
>, and <A
HREF="system.html#LASTCOMMREF"
>lastcomm</A
> to aid your
surveillance of the suspected fiend.</P
></DD
><DT
><B
CLASS="COMMAND"
>Checking for Broken Links</B
></DT
><DD
><P
>Using <A
HREF="communications.html#LYNXREF"
>lynx</A
> with the
<TT
CLASS="OPTION"
>-traversal</TT
> option, write a script that
checks a Web site for broken links.</P
></DD
></DL
></DIV
><DIV
CLASS="VARIABLELIST"
><P
><B
><A
NAME="EXDIFFICULT1"
></A
>DIFFICULT</B
></P
><DL
><DT
><B
CLASS="COMMAND"
>Testing Passwords</B
></DT
><DD
><P
>Write a script to check and validate passwords. The object
is to flag <SPAN
CLASS="QUOTE"
>"weak"</SPAN
> or easily guessed password
candidates.</P
><P
>A trial password will be input to the script as a
command-line parameter. To be considered acceptable,
a password must meet the following minimum qualifications:
<UL
><LI
><P
>Minimum length of 8 characters</P
></LI
><LI
><P
>Must contain at least one numeric character</P
></LI
><LI
><P
>Must contain at least one of the following
non-alphabetic characters: <SPAN
CLASS="TOKEN"
>@</SPAN
>,
<SPAN
CLASS="TOKEN"
>#</SPAN
>, <SPAN
CLASS="TOKEN"
>$</SPAN
>, <SPAN
CLASS="TOKEN"
>%</SPAN
>,
<SPAN
CLASS="TOKEN"
>&</SPAN
>, <SPAN
CLASS="TOKEN"
>*</SPAN
>, <SPAN
CLASS="TOKEN"
>+</SPAN
>,
<SPAN
CLASS="TOKEN"
>-</SPAN
>, <SPAN
CLASS="TOKEN"
>=</SPAN
></P
></LI
></UL
></P
><P
>Optional:
<UL
><LI
><P
>Do a dictionary check on every sequence of at least
four consecutive alphabetic characters in the password under
test. This will eliminate passwords containing embedded
<SPAN
CLASS="QUOTE"
>"words"</SPAN
> found in a standard dictionary.</P
></LI
><LI
><P
>Enable the script to check all the passwords on your
system. These do not reside in
<A
HREF="files.html#DATAFILESREF1"
><TT
CLASS="FILENAME"
>/etc/passwd</TT
></A
>.</P
></LI
></UL
></P
><P
>This exercise tests mastery of <A
HREF="regexp.html#REGEXREF"
>Regular Expressions</A
>.</P
></DD
><DT
><B
CLASS="COMMAND"
>Cross Reference</B
></DT
><DD
><P
>Write a script that generates a
<I
CLASS="FIRSTTERM"
>cross-reference</I
>
(<I
CLASS="FIRSTTERM"
>concordance</I
>) on a target file.
The output will be a listing of all word occurrences in
the target file, along with the line numbers in which
each word occurs. Traditionally, <I
CLASS="FIRSTTERM"
>linked
list</I
> constructs would be used in such
applications. Therefore, you should investigate <A
HREF="arrays.html#ARRAYREF"
>arrays</A
> in the course of
this exercise. <A
HREF="textproc.html#WF"
>Example 16-12</A
> is probably
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>not</I
></SPAN
> a good place to start.</P
></DD
><DT
><A
NAME="NEWTONSQRT"
></A
><B
CLASS="COMMAND"
>Square Root</B
></DT
><DD
><P
>Write a script to calculate square roots of numbers
using <I
CLASS="FIRSTTERM"
>Newton's Method</I
>.</P
><P
>The algorithm for this, expressed as a snippet of Bash
<A
HREF="assortedtips.html#PSEUDOCODEREF"
>pseudo-code</A
> is:</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 # (Isaac) Newton's Method for speedy extraction
2 #+ of square roots.
3
4 guess = $argument
5 # $argument is the number to find the square root of.
6 # $guess is each successive calculated "guess" -- or trial solution --
7 #+ of the square root.
8 # Our first "guess" at a square root is the argument itself.
9
10 oldguess = 0
11 # $oldguess is the previous $guess.
12
13 tolerance = .000001
14 # To how close a tolerance we wish to calculate.
15
16 loopcnt = 0
17 # Let's keep track of how many times through the loop.
18 # Some arguments will require more loop iterations than others.
19
20
21 while [ ABS( $guess $oldguess ) -gt $tolerance ]
22 # ^^^^^^^^^^^^^^^^^^^^^^^ Fix up syntax, of course.
23
24 # "ABS" is a (floating point) function to find the absolute value
25 #+ of the difference between the two terms.
26 # So, as long as difference between current and previous
27 #+ trial solution (guess) exceeds the tolerance, keep looping.
28
29 do
30 oldguess = $guess # Update $oldguess to previous $guess.
31
32 # =======================================================
33 guess = ( $oldguess + ( $argument / $oldguess ) ) / 2.0
34 # = 1/2 ( ($oldguess **2 + $argument) / $oldguess )
35 # equivalent to:
36 # = 1/2 ( $oldguess + $argument / $oldguess )
37 # that is, "averaging out" the trial solution and
38 #+ the proportion of argument deviation
39 #+ (in effect, splitting the error in half).
40 # This converges on an accurate solution
41 #+ with surprisingly few loop iterations . . .
42 #+ for arguments > $tolerance, of course.
43 # =======================================================
44
45 (( loopcnt++ )) # Update loop counter.
46 done</PRE
></TD
></TR
></TABLE
></P
><P
>It's a simple enough recipe, and
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>seems</I
></SPAN
> at first glance easy enough to
convert into a working Bash script. The problem, though,
is that Bash has <A
HREF="operations.html#NOFLOATINGPOINT"
>no native
support for floating point numbers</A
>. So, the script
writer needs to use <A
HREF="mathc.html#BCREF"
>bc</A
> or
possibly <A
HREF="awk.html#AWKREF"
>awk</A
> to convert the
numbers and do the calculations. It could get rather messy
. . .</P
></DD
><DT
><B
CLASS="COMMAND"
>Logging File Accesses</B
></DT
><DD
><P
>Log all accesses to the files in <TT
CLASS="FILENAME"
>/etc</TT
> during the course of
a single day. This information should include the filename,
user name, and access time. If any alterations to the
files take place, that will be flagged. Write this data
as tabular (tab-separated) formatted records in a logfile.</P
></DD
><DT
><B
CLASS="COMMAND"
>Monitoring Processes</B
></DT
><DD
><P
>Write a script to continually monitor all running
processes and to keep track of how many child processes each
parent spawns. If a process spawns more than five children,
then the script sends an e-mail to the system administrator
(or <I
CLASS="FIRSTTERM"
>root</I
>) with all relevant
information, including the time, PID of the parent, PIDs
of the children, etc. The script appends a report to a log
file every ten minutes. </P
></DD
><DT
><B
CLASS="COMMAND"
>Strip Comments</B
></DT
><DD
><P
>Strip all comments from a shell script whose name
is specified on the command-line. Note that the initial
<A
HREF="sha-bang.html#SHABANGREF"
>#! line</A
> must not be
stripped out.</P
></DD
><DT
><B
CLASS="COMMAND"
>Strip HTML Tags</B
></DT
><DD
><P
>Strip all the HTML tags from a specified HTML file, then
reformat it into lines between 60 and 75 characters
in length. Reset paragraph and block spacing, as
appropriate, and convert HTML tables to their approximate
text equivalent.</P
></DD
><DT
><B
CLASS="COMMAND"
>XML Conversion</B
></DT
><DD
><P
>Convert an XML file to both HTML and text format.</P
><P
>Optional: A script that converts Docbook/SGML to XML.</P
></DD
><DT
><A
NAME="CSPAMMERS"
></A
><B
CLASS="COMMAND"
>Chasing Spammers</B
></DT
><DD
><P
> Write a script that analyzes a spam e-mail by doing
DNS lookups on the IP addresses in the headers to identify
the relay hosts as well as the originating ISP. The
script will forward the unaltered spam message to the
responsible ISPs. Of course, it will be necessary to
filter out <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>your own ISP's IP address</I
></SPAN
>,
so you don't end up complaining about yourself.</P
><P
>As necessary, use the appropriate <A
HREF="communications.html#COMMUNINFO1"
>network analysis commands</A
>.</P
><P
>For some ideas, see <A
HREF="communications.html#ISSPAMMER"
>Example 16-41</A
> and <A
HREF="contributed-scripts.html#ISSPAMMER2"
>Example A-28</A
>.</P
><P
>Optional: Write a script that searches through a list of
e-mail messages and deletes the spam according to specified
filters.</P
></DD
><DT
><B
CLASS="COMMAND"
>Creating man pages</B
></DT
><DD
><P
>Write a script that automates the process of creating
<A
HREF="external.html#MANREF"
>man pages</A
>.</P
><P
>Given a text file which contains information to be
formatted into a <I
CLASS="FIRSTTERM"
>man page</I
>, the
script will read the file, then invoke the appropriate
<A
HREF="textproc.html#GROFFREF"
>groff</A
> commands to
output the corresponding <I
CLASS="FIRSTTERM"
>man page</I
>
to <TT
CLASS="FILENAME"
>stdout</TT
>. The text file contains
blocks of information under the standard <I
CLASS="FIRSTTERM"
>man
page</I
> headings, i.e., NAME, SYNOPSIS,
DESCRIPTION, etc.</P
><P
><A
HREF="contributed-scripts.html#MANED"
>Example A-39</A
> is an instructive first step.</P
></DD
><DT
><B
CLASS="COMMAND"
>Hex Dump</B
></DT
><DD
><P
>Do a hex(adecimal) dump on a binary file
specified as an argument to the script. The output should
be in neat tabular <A
HREF="special-chars.html#FIELDREF"
>fields</A
>,
with the first field showing the address, each of the
next 8 fields a 4-byte hex number, and the final field
the ASCII equivalent of the previous 8 fields.</P
><P
>The obvious followup to this is to extend the hex
dump script into a disassembler. Using a lookup table,
or some other clever gimmick, convert the hex values into
80x86 op codes.</P
></DD
><DT
><B
CLASS="COMMAND"
>Emulating a Shift Register</B
></DT
><DD
><P
>Using <A
HREF="arrays.html#STACKEX"
>Example 27-15</A
> as an inspiration,
write a script that emulates a 64-bit shift register as
an <A
HREF="arrays.html#ARRAYREF"
>array</A
>. Implement
functions to <I
CLASS="FIRSTTERM"
>load</I
> the register,
<I
CLASS="FIRSTTERM"
>shift left</I
>, <I
CLASS="FIRSTTERM"
>shift
right</I
>, and <I
CLASS="FIRSTTERM"
>rotate</I
>
it. Finally, write a function that interprets the register
contents as eight 8-bit ASCII characters.</P
></DD
><DT
><B
CLASS="COMMAND"
>Calculating Determinants</B
></DT
><DD
><P
>Write a script that calculates
determinants
<A
NAME="AEN25254"
HREF="#FTN.AEN25254"
>[1]</A
>
by <A
HREF="localvar.html#RECURSIONREF0"
>recursively</A
> expanding the
<I
CLASS="FIRSTTERM"
>minors</I
>. Use a 4 x 4 determinant as
a test case.</P
></DD
><DT
><B
CLASS="COMMAND"
>Hidden Words</B
></DT
><DD
><P
>Write a <SPAN
CLASS="QUOTE"
>"word-find"</SPAN
> puzzle generator,
a script that hides 10 input words in a 10 x 10 array
of random letters. The words may be hidden across, down,
or diagonally.</P
><P
>Optional: Write a script that <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>solves</I
></SPAN
>
word-find puzzles. To keep this from becoming too difficult,
the solution script will find only horizontal and vertical
words. (Hint: Treat each row and column as a string, and
search for substrings.)</P
></DD
><DT
><B
CLASS="COMMAND"
>Anagramming</B
></DT
><DD
><P
> Anagram 4-letter input. For example, the
anagrams of <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>word</I
></SPAN
> are:
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>do or rod row word</I
></SPAN
>. You may use
<TT
CLASS="FILENAME"
>/usr/share/dict/linux.words</TT
> as the
reference list.</P
></DD
><DT
><B
CLASS="COMMAND"
>Word Ladders</B
></DT
><DD
><P
>A <SPAN
CLASS="QUOTE"
>"word ladder"</SPAN
> is a sequence of words,
with each successive word in the sequence differing from
the previous one by a single letter.</P
><P
>For example, to <SPAN
CLASS="QUOTE"
>"ladder"</SPAN
> from
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>mark</I
></SPAN
> to
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>vase</I
></SPAN
>:</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 mark --> park --> part --> past --> vast --> vase
2 ^ ^ ^ ^ ^</PRE
></TD
></TR
></TABLE
>
</P
><P
>Write a script that solves word ladder puzzles. Given
a starting and an ending word, the script will list all
intermediate steps in the <SPAN
CLASS="QUOTE"
>"ladder."</SPAN
> Note
that <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>all</I
></SPAN
> words in the sequence must
be legitimate dictionary words.</P
></DD
><DT
><B
CLASS="COMMAND"
>Fog Index</B
></DT
><DD
><P
>The <SPAN
CLASS="QUOTE"
>"fog index"</SPAN
> of a passage of text
estimates its reading difficulty, as a number corresponding
roughly to a school grade level. For example, a passage
with a fog index of 12 should be comprehensible to anyone
with 12 years of schooling.</P
><P
>The Gunning version of the fog index uses the following
algorithm.</P
><OL
TYPE="1"
><LI
><P
>Choose a section of the text at least
100 words in length.</P
></LI
><LI
><P
>Count the number of sentences (a portion of
a sentence truncated by the boundary of the text section
counts as one).</P
></LI
><LI
><P
>Find the average number of words per
sentence.</P
><P
>AVE_WDS_SEN = TOTAL_WORDS / SENTENCES</P
></LI
><LI
><P
>Count the number of <SPAN
CLASS="QUOTE"
>"difficult"</SPAN
>
words in the segment -- those containing at least
3 syllables. Divide this quantity by total words to
get the proportion of difficult words.</P
><P
>PRO_DIFF_WORDS = LONG_WORDS / TOTAL_WORDS</P
></LI
><LI
><P
>The Gunning fog index is the sum of the above two
quantities, multiplied by 0.4, then rounded to the
nearest integer.</P
><P
>G_FOG_INDEX = int ( 0.4 * ( AVE_WDS_SEN + PRO_DIFF_WORDS ) )</P
></LI
></OL
><P
>Step 4 is by far the most difficult portion of the
exercise. There exist various algorithms for estimating
the syllable count of a word. A rule-of-thumb formula
might consider the number of letters in a word and the
vowel-consonant mix.</P
><P
>A strict interpretation of the Gunning fog index does
not count compound words and proper nouns as
<SPAN
CLASS="QUOTE"
>"difficult"</SPAN
> words, but this would enormously
complicate the script.</P
></DD
><DT
><B
CLASS="COMMAND"
>Calculating PI using Buffon's Needle</B
></DT
><DD
><P
>The Eighteenth Century French mathematician de Buffon
came up with a novel experiment. Repeatedly drop a needle
of length <TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
> onto a wooden floor
composed of long and narrow parallel boards. The cracks
separating the equal-width floorboards are a fixed distance
<TT
CLASS="REPLACEABLE"
><I
>d</I
></TT
> apart. Keep track of the
total drops and the number of times the needle intersects
a crack on the floor. The ratio of these two quantities
turns out to be a fractional multiple of PI.</P
><P
>In the spirit of <A
HREF="mathc.html#CANNON"
>Example 16-50</A
>, write a
script that runs a Monte Carlo simulation of
<I
CLASS="FIRSTTERM"
>Buffon's Needle</I
>. To simplify matters,
set the needle length equal to the distance between the
cracks, <TT
CLASS="PARAMETER"
><I
>n = d</I
></TT
>.</P
><P
>Hint: there are actually two critical variables:
the distance from the center of the needle to the nearest
crack, and the inclination angle of the needle to that crack.
You may use <A
HREF="mathc.html#BCREF"
>bc</A
> to handle
the calculations.</P
></DD
><DT
><B
CLASS="COMMAND"
>Playfair Cipher</B
></DT
><DD
><P
>Implement the Playfair (Wheatstone) Cipher in a
script.</P
><P
>The Playfair Cipher encrypts text by substitution
of <I
CLASS="FIRSTTERM"
>digrams</I
> (2-letter groupings).
It is traditional to use a 5 x 5 letter scrambled-alphabet
<I
CLASS="FIRSTTERM"
>key square</I
> for the encryption and
decryption.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 C O D E S
2 A B F G H
3 I K L M N
4 P Q R T U
5 V W X Y Z
6
7 Each letter of the alphabet appears once, except "I" also represents
8 "J". The arbitrarily chosen key word, "CODES" comes first, then all
9 the rest of the alphabet, in order from left to right, skipping letters
10 already used.
11
12 To encrypt, separate the plaintext message into digrams (2-letter
13 groups). If a group has two identical letters, delete the second, and
14 form a new group. If there is a single letter left over at the end,
15 insert a "null" character, typically an "X."
16
17 THIS IS A TOP SECRET MESSAGE
18
19 TH IS IS AT OP SE CR ET ME SA GE
20
21
22
23 For each digram, there are three possibilities.
24 -----------------------------------------------
25
26 1) Both letters will be on the same row of the key square:
27 For each letter, substitute the one immediately to the right, in that
28 row. If necessary, wrap around left to the beginning of the row.
29
30 or
31
32 2) Both letters will be in the same column of the key square:
33 For each letter, substitute the one immediately below it, in that
34 row. If necessary, wrap around to the top of the column.
35
36 or
37
38 3) Both letters will form the corners of a rectangle within the key square:
39 For each letter, substitute the one on the other corner the rectangle
40 which lies on the same row.
41
42
43 The "TH" digram falls under case #3.
44 G H
45 M N
46 T U (Rectangle with "T" and "H" at corners)
47
48 T --> U
49 H --> G
50
51
52 The "SE" digram falls under case #1.
53 C O D E S (Row containing "S" and "E")
54
55 S --> C (wraps around left to beginning of row)
56 E --> S
57
58 =========================================================================
59
60 To decrypt encrypted text, reverse the above procedure under cases #1
61 and #2 (move in opposite direction for substitution). Under case #3,
62 just take the remaining two corners of the rectangle.
63
64
65 Helen Fouche Gaines' classic work, ELEMENTARY CRYPTANALYSIS (1939), gives a
66 fairly detailed description of the Playfair Cipher and its solution methods.</PRE
></TD
></TR
></TABLE
>
</P
><P
>This script will have three main sections</P
><OL
TYPE="I"
><LI
><P
>Generating the <I
CLASS="FIRSTTERM"
>key square</I
>,
based on a user-input keyword.</P
></LI
><LI
><P
>Encrypting a <I
CLASS="FIRSTTERM"
>plaintext</I
>
message.</P
></LI
><LI
><P
>Decrypting encrypted
text.</P
></LI
></OL
><P
>The script will make extensive use of <A
HREF="arrays.html#ARRAYREF"
>arrays</A
> and <A
HREF="functions.html#FUNCTIONREF"
>functions</A
>.
You may use <A
HREF="contributed-scripts.html#GRONSFELD"
>Example A-56</A
> as an
inspiration.</P
></DD
></DL
></DIV
><P
>--</P
><P
>Please do not send the author your solutions to these
exercises. There are more appropriate ways to impress him with
your cleverness, such as submitting bugfixes and suggestions
for improving the book.</P
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN25254"
HREF="writingscripts.html#AEN25254"
>[1]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>For all you clever types who failed intermediate algebra,
a <I
CLASS="FIRSTTERM"
>determinant</I
> is a numerical value
associated with a multidimensional
<I
CLASS="FIRSTTERM"
>matrix</I
> (<A
HREF="arrays.html#ARRAYREF"
>array</A
> of numbers).
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 For the simple case of a 2 x 2 determinant:
2
3 |a b|
4 |b a|
5
6 The solution is a*a - b*b, where "a" and "b" represent numbers.</PRE
></TD
></TR
></TABLE
>
</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="exercises.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="revisionhistory.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Exercises</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="exercises.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Revision History</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|