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 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<!--Converted with LaTeX2HTML 99.2beta8 (1.46)
original version by: Nikos Drakos, CBLU, University of Leeds
* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>20. Advanced Shell Scripting</TITLE>
<META NAME="description" CONTENT="20. Advanced Shell Scripting">
<META NAME="keywords" CONTENT="rute">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="LaTeX2HTML v99.2beta8">
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
<LINK REL="STYLESHEET" HREF="rute.css">
<LINK REL="next" HREF="node24.html">
<LINK REL="previous" HREF="node22.html">
<LINK REL="up" HREF="rute.html">
<LINK REL="next" HREF="node24.html">
</HEAD>
<BODY BGCOLOR=#FFFFFF >
<TABLE width="100%" border="0" cellspacing="0" cellpadding="0">
<TR><TD align=left bgcolor="#000000">
<FONT COLOR=white>
<A HREF="http://www.icon.co.za/~psheer/rute-purchase.html"><FONT COLOR=white>Purchase</FONT></A>
</FONT>
</TD><TD align=center bgcolor="#000000">
<FONT COLOR=white>
Copyright © 2002 Paul Sheer. <A HREF="copying.html"><FONT COLOR=white>Click here for copying permissions.</FONT></A>
</FONT>
</TD><TD align=right bgcolor="#000000">
<FONT COLOR=white>
<A HREF="http://www.icon.co.za/~psheer/rute-home.html"><FONT COLOR=white>Home</FONT></A>
</FONT>
</TD></TR>
<TR><TD colspan=2 align=left bgcolor="#ECEBF4">
<IMG SRC="va-btn-small-light-60.png">
</TD><TD align=right bgcolor="#ECEBF4">
<IMG SRC="sflogo2-steel-60.png">
</TD></TR>
</TABLE><BR>
<!--Navigation Panel-->
<A NAME="tex2html2123"
HREF="node24.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A>
<A NAME="tex2html2119"
HREF="rute.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A>
<A NAME="tex2html2113"
HREF="node22.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A>
<A NAME="tex2html2121"
HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A>
<BR>
<B> Next:</B> <A NAME="tex2html2124"
HREF="node24.html">21. System Services and</A>
<B> Up:</B> <A NAME="tex2html2120"
HREF="rute.html">rute</A>
<B> Previous:</B> <A NAME="tex2html2114"
HREF="node22.html">19. Partitions, File Systems,</A>
  <B> <A NAME="tex2html2122"
HREF="node1.html">Contents</A></B>
<BR>
<BR>
<!--End of Navigation Panel-->
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>
<UL>
<LI><A NAME="tex2html2125"
HREF="#SECTION002310000000000000000">20.1 Lists of Commands</A>
<LI><A NAME="tex2html2126"
HREF="#SECTION002320000000000000000">20.2 Special Parameters: <TT>
<FONT COLOR="#0000ff">$?</FONT></TT>, <TT>
<FONT COLOR="#0000ff">$*</FONT></TT>,...</A>
<LI><A NAME="tex2html2127"
HREF="#SECTION002330000000000000000">20.3 Expansion</A>
<LI><A NAME="tex2html2128"
HREF="#SECTION002340000000000000000">20.4 Built-in Commands</A>
<LI><A NAME="tex2html2129"
HREF="#SECTION002350000000000000000">20.5 Trapping Signals -- the <TT>
<FONT COLOR="#0000ff">trap</FONT></TT> Command</A>
<LI><A NAME="tex2html2130"
HREF="#SECTION002360000000000000000">20.6 Internal Settings -- the <TT>
<FONT COLOR="#0000ff">set</FONT></TT> Command</A>
<LI><A NAME="tex2html2131"
HREF="#SECTION002370000000000000000">20.7 Useful Scripts and Commands</A>
<UL>
<LI><A NAME="tex2html2132"
HREF="#SECTION002371000000000000000">20.7.1 <TT>
<FONT COLOR="#0000ff">chroot</FONT></TT></A>
<LI><A NAME="tex2html2133"
HREF="#SECTION002372000000000000000">20.7.2 <TT>
<FONT COLOR="#0000ff">if</FONT></TT> conditionals</A>
<LI><A NAME="tex2html2134"
HREF="#SECTION002373000000000000000">20.7.3 <TT>
<FONT COLOR="#0000ff">patch</FONT></TT>ing and <TT>
<FONT COLOR="#0000ff">diff</FONT></TT>ing</A>
<LI><A NAME="tex2html2135"
HREF="#SECTION002374000000000000000">20.7.4 Internet connectivity test</A>
<LI><A NAME="tex2html2136"
HREF="#SECTION002375000000000000000">20.7.5 Recursive <TT>
<FONT COLOR="#0000ff">grep</FONT></TT> (search)</A>
<LI><A NAME="tex2html2137"
HREF="#SECTION002376000000000000000">20.7.6 Recursive search and replace</A>
<LI><A NAME="tex2html2138"
HREF="#SECTION002377000000000000000">20.7.7 <TT>
<FONT COLOR="#0000ff">cut</FONT></TT> and <TT>
<FONT COLOR="#0000ff">awk</FONT></TT> -- manipulating text file fields</A>
<LI><A NAME="tex2html2139"
HREF="#SECTION002378000000000000000">20.7.8 Calculations with <TT>
<FONT COLOR="#0000ff">bc</FONT></TT></A>
<LI><A NAME="tex2html2140"
HREF="#SECTION002379000000000000000">20.7.9 Conversion of graphics formats of many files</A>
<LI><A NAME="tex2html2141"
HREF="#SECTION0023710000000000000000">20.7.10 Securely erasing files</A>
<LI><A NAME="tex2html2142"
HREF="#SECTION0023711000000000000000">20.7.11 Persistent background processes</A>
<LI><A NAME="tex2html2143"
HREF="#SECTION0023712000000000000000">20.7.12 Processing the process list</A>
</UL>
<LI><A NAME="tex2html2144"
HREF="#SECTION002380000000000000000">20.8 Shell Initialization</A>
<UL>
<LI><A NAME="tex2html2145"
HREF="#SECTION002381000000000000000">20.8.1 Customizing the <TT>
<FONT COLOR="#0000ff">PATH</FONT></TT> and <TT>
<FONT COLOR="#0000ff">LD_LIBRARY_PATH</FONT></TT></A>
</UL>
<LI><A NAME="tex2html2146"
HREF="#SECTION002390000000000000000">20.9 File Locking</A>
<UL>
<LI><A NAME="tex2html2147"
HREF="#SECTION002391000000000000000">20.9.1 Locking a mailbox file</A>
<LI><A NAME="tex2html2148"
HREF="#SECTION002392000000000000000">20.9.2 Locking over NFS</A>
<LI><A NAME="tex2html2149"
HREF="#SECTION002393000000000000000">20.9.3 Directory versus file locking</A>
<LI><A NAME="tex2html2150"
HREF="#SECTION002394000000000000000">20.9.4 Locking inside <B>C</B> programs</A>
</UL></UL>
<!--End of Table of Child-Links-->
<HR>
<H1><A NAME="SECTION002300000000000000000">
20. Advanced Shell Scripting</A>
</H1>
<P>
<A NAME="chap:advancedscript"></A>
<P>
This chapter completes our discussion of <TT>
<FONT COLOR="#0000ff">sh</FONT></TT> shell scripting
begun in Chapter <A HREF="node10.html#chap:shellscript">7</A> and expanded on in
Chapter <A HREF="node12.html#chap:processenviron">9</A>. These three chapters represent
almost everything you can do with the <TT>
<FONT COLOR="#0000ff">bash</FONT></TT> shell.
<P>
<H1><A NAME="SECTION002310000000000000000">
20.1 Lists of Commands</A>
</H1>
<P>
The special operator <TT>
<FONT COLOR="#0000ff">&&</FONT></TT> and <TT>
<FONT COLOR="#0000ff">||</FONT></TT> can be used to
execute functions in sequence. For instance:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>grep '^harry:' /etc/passwd || useradd harry</code><br>
</FONT></TD></TR></TABLE><P>
The <TT>
<FONT COLOR="#0000ff">||</FONT></TT> means to only execute the second
command if the first command returns an error. In the above
case, <TT>
<FONT COLOR="#0000ff">grep</FONT></TT> will return an exit code of 1 if <TT>
<FONT COLOR="#0000ff">harry</FONT></TT>
is not in the <TT>
<FONT COLOR="#0000ff">/etc/passwd</FONT></TT> file, causing <TT>
<FONT COLOR="#0000ff">useradd</FONT></TT>
to be executed.
<P>
An alternate representation is
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>grep -v '^harry:' /etc/passwd && useradd harry</code><br>
</FONT></TD></TR></TABLE><P>
where the <TT>
<FONT COLOR="#0000ff">-v</FONT></TT> option inverts the sense of matching
of <TT>
<FONT COLOR="#0000ff">grep</FONT></TT>. <TT>
<FONT COLOR="#0000ff">&&</FONT></TT> has the opposite meaning to <TT>
<FONT COLOR="#0000ff">||</FONT></TT>,
that is, to execute the second command only if the first succeeds.
<P>
Adept script writers often string together many commands
to create the most succinct representation of an operation:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>grep -v '^harry:' /etc/passwd && useradd harry || \</code><br>
<code> echo "`date`: useradd failed" >> /var/log/my_special_log</code><br>
</FONT></TD></TR></TABLE><P>
<P>
<H1><A NAME="SECTION002320000000000000000">
20.2 Special Parameters: <TT>
<FONT COLOR="#0000ff">$?</FONT></TT>, <TT>
<FONT COLOR="#0000ff">$*</FONT></TT>,...</A>
</H1>
<P>
An ordinary variable can be expanded with <TT>
<FONT COLOR="#0000ff">$</FONT></TT><I>VARNAME</I>.
Commonly used variables like <TT>
<FONT COLOR="#0000ff">PATH</FONT></TT> and special variables
like <TT>
<FONT COLOR="#0000ff">PWD</FONT></TT> and <TT>
<FONT COLOR="#0000ff">RANDOM</FONT></TT> were covered in Chapter
<A HREF="node12.html#chap:processenviron">9</A>. Further special expansions are
documented in the following section, quoted verbatim from the
<TT>
<FONT COLOR="#0000ff">bash</FONT></TT> man page (the footnotes are
mine).<FONT COLOR="#ffa500">(footnote follows)</FONT> <FONT COLOR="#ffa500">[Thanks to Brian Fox and Chet Ramey for this material.]</FONT>
<P>
<BLOCKQUOTE><FONT SIZE="-1"><B>Special Parameters</B>
<BR>
<BR>
The shell treats several parameters specially. These
parameters may only be referenced; assignment to them is
not allowed.
</FONT></BLOCKQUOTE><DL>
<DT><STRONG><B>$*</B></STRONG></DT>
<DD>Expands to the positional parameters (i.e., the command-line arguments
passed to the shell script, with <B>$1</B> being the
first argument, <B>$2</B> the second etc.), starting from
one. When the expansion occurs within double
quotes, it expands to a single word with the value
of each parameter separated by the first character
of the <B>IFS</B> special variable. That is, "<B>$*</B>" is
equivalent to "<B>$1</B><I>c</I><B>$2</B><I>c</I><B>...</B>", where <I>c</I> is the first
character of the value of the <B>IFS</B> variable. If <B>IFS</B>
is unset, the parameters are separated by spaces.
If <B>IFS</B> is null, the parameters are joined without
intervening separators.
</DD>
<DT><STRONG><B>$@</B></STRONG></DT>
<DD>Expands to the positional parameters, starting from
one. When the expansion occurs within double
quotes, each parameter expands to a separate word.
That is, "<B>$@</B>" is equivalent to "<B>$1</B>" "<B>$2</B>" ... When
there are no positional parameters, "<B>$@</B>" and <B>$@</B>
expand to nothing (i.e., they are removed). <FONT COLOR="#ffa500">[Hint: this is
very useful for writing wrapper shell scripts that just add
one argument.]</FONT>
</DD>
<DT><STRONG><B>$#</B></STRONG></DT>
<DD>Expands to the number of positional parameters in
decimal (i.e. the number of command-line arguments).
</DD>
<DT><STRONG><B>$?</B></STRONG></DT>
<DD>Expands to the status of the most recently executed
foreground pipeline. <FONT COLOR="#ffa500">[I.e., the exit code of the last
command.]</FONT>
</DD>
<DT><STRONG><B>$-</B></STRONG></DT>
<DD>Expands to the current option flags as specified
upon invocation, by the <B>set</B> builtin command, or
those set by the shell itself (such as the <B>-i</B>
option).
</DD>
<DT><STRONG><B>$$</B></STRONG></DT>
<DD>Expands to the process ID of the shell. In a ()
subshell, it expands to the process ID of the current
shell, not the subshell.
</DD>
<DT><STRONG><B>$!</B></STRONG></DT>
<DD>Expands to the process ID of the most recently executed
background (asynchronous) command. <FONT COLOR="#ffa500">[I.e., after executing
a background command with <I>command </I><B>&</B>, the variable
<B>$!</B> will give its process ID.]</FONT>
</DD>
<DT><STRONG><B>$0</B></STRONG></DT>
<DD>Expands to the name of the shell or shell script.
This is set at shell initialization. If <B>bash</B> is
invoked with a file of commands, <B>$0</B> is set to the
name of that file. If <B>bash</B> is started with the <B>-c</B>
option, then <B>$0</B> is set to the first argument after
the string to be executed, if one is present. Otherwise,
it is set to the file name used to invoke
<B>bash</B>, as given by argument zero. <FONT COLOR="#ffa500">[Note that <B>basename $0</B>
is a useful way to get the name of the current command without the leading
path.]</FONT>
</DD>
<DT><STRONG><B>$-</B></STRONG></DT>
<DD>At shell startup, set to the absolute file name of
the shell or shell script being executed as passed
in the argument list. Subsequently, expands to the
last argument to the previous command, after expansion.
Also set to the full file name of each command
executed and placed in the environment
exported to that command. When checking mail, this
parameter holds the name of the mail file currently
being checked.
</DD>
</DL><BLOCKQUOTE></BLOCKQUOTE>
<P>
<H1><A NAME="SECTION002330000000000000000">
20.3 Expansion</A>
</H1>
<P>
<I>Expansion</I> refers to the way <TT>
<FONT COLOR="#0000ff">bash</FONT></TT> modifies
the command-line before executing it. <TT>
<FONT COLOR="#0000ff">bash</FONT></TT> performs several
textual modifications to the command-line, proceeding in the following order:
<DL>
<DT><STRONG><I>Brace expansion</I></STRONG></DT>
<DD>We have already shown how you can use, for example,
the shorthand <TT>
<FONT COLOR="#0000ff">touch file_{one,two,three}.txt</FONT></TT> to create multiple
files <TT>
<FONT COLOR="#0000ff">file_one.txt</FONT></TT>, <TT>
<FONT COLOR="#0000ff">file_two.txt</FONT></TT>, and <TT>
<FONT COLOR="#0000ff">file_three.txt</FONT></TT>.
This is known as brace expansion and occurs before any other kind of modification
to the command-line.
</DD>
<DT><STRONG><I>Tilde expansion</I></STRONG></DT>
<DD>The special character <TT>
<FONT COLOR="#0000ff">~</FONT></TT> is replaced with
the full path contained in the <TT>
<FONT COLOR="#0000ff">HOME</FONT></TT> environment variable or the home
directory of the users login (if <TT>
<FONT COLOR="#0000ff">$HOME</FONT></TT> is null). <TT>
<FONT COLOR="#0000ff">~+</FONT></TT> is replaced with
the current working directory and <TT>
<FONT COLOR="#0000ff">~-</FONT></TT> is replaced with the most recent
previous working directory. The last two are rarely used.
</DD>
<DT><STRONG><I>Parameter expansion</I></STRONG></DT>
<DD>This refers to expanding anything that begins
with a <TT>
<FONT COLOR="#0000ff">$</FONT></TT>. Note that <TT>
<FONT COLOR="#0000ff">$</FONT></TT><I>VAR</I> and <TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT>
do exactly the same thing, except in the latter case, <I>VAR</I> can contain
non-``whole word'' characters that would normally confuse <TT>
<FONT COLOR="#0000ff">bash</FONT></TT>.
<P>
There are several parameter expansion tricks that you can use to do string manipulation.
Most shell programmers never bother with these, probably because
they are not well supported by other U<SMALL>NIX</SMALL> systems.
<DL>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">:-</FONT></TT><I>default</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT></STRONG></DT>
<DD>This will result in
<TT>
<FONT COLOR="#0000ff">$</FONT></TT><I>VAR</I> unless <I>VAR</I> is unset or null, in which case
it will result in <I>default</I>.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">:=</FONT></TT><I>default</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT></STRONG></DT>
<DD>Same as previous
except that <I>default</I> is also assigned to <TT>
<FONT COLOR="#0000ff">VAR</FONT></TT> if it is empty.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">:-</FONT></TT><I>default</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT></STRONG></DT>
<DD>This will result in
an empty string if <I>VAR</I> is unset or null; otherwise it will result in
<I>default</I>. This is the opposite behavior of
<TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">:-</FONT></TT><I>default</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT>.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">:?</FONT></TT><I>message</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT></STRONG></DT>
<DD>This will result in
<TT>
<FONT COLOR="#0000ff">$</FONT></TT><I>VAR</I> unless <I>VAR</I> is unset or null, in which case
an error message containing <I>message</I> is displayed.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">:</FONT></TT><I>offset</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT> or <TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">:</FONT></TT><I>n</I><TT>
<FONT COLOR="#0000ff">:</FONT></TT><I>l</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT></STRONG></DT>
<DD>This produces the <I>n</I>th character of <TT>
<FONT COLOR="#0000ff">$</FONT></TT><I>VAR</I> and then the following
<I>l</I> characters. If <I>l</I> is not present, then all characters to the right of
the <I>n</I>th character are produced. This is useful for splitting up strings. Try:
</DD>
</DL>
</DD>
</DL>
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>TEXT=scripting_for_phun</code><br>
<code>echo ${TEXT:10:3}</code><br>
<code>echo ${TEXT:10}</code><br>
</FONT></TD></TR></TABLE><P>
<DL>
<DT><STRONG> </STRONG></DT>
<DD>
<BR>
<DL>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">${#</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT></STRONG></DT>
<DD>Gives the length of <TT>
<FONT COLOR="#0000ff">$</FONT></TT><I>VAR</I>.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">${!</FONT></TT><I>PRE</I><TT>
<FONT COLOR="#0000ff">*}</FONT></TT></STRONG></DT>
<DD>Gives a list of all variables whose
names begin with <I>PRE</I>.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">#</FONT></TT><I>pattern</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT></STRONG></DT>
<DD><TT>
<FONT COLOR="#0000ff">$</FONT></TT><I>VAR</I>
is returned with the glob expression <I>pattern</I> removed from the leading
part of the string. For instance, <TT>
<FONT COLOR="#0000ff">${TEXT#scr}</FONT></TT> in the above example
will return <TT>
<FONT COLOR="#0000ff">ripting_for_phun</FONT></TT>.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">##</FONT></TT><I>pattern</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT></STRONG></DT>
<DD>This is the same as
the previous expansion except that if <I>pattern</I> contains wild cards, then it
will try to match the maximum length of characters.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">%</FONT></TT><I>pattern</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT></STRONG></DT>
<DD>The same as
<TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">#</FONT></TT><I>pattern</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT> except that characters are
removed from the trailing part of the string.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">%%</FONT></TT><I>pattern</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT></STRONG></DT>
<DD>The same as
<TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">##</FONT></TT><I>pattern</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT> except that characters are
removed from the trailing part of the string.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">/</FONT></TT><I>search</I><TT>
<FONT COLOR="#0000ff">/</FONT></TT><I>replace</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT></STRONG></DT>
<DD><TT>
<FONT COLOR="#0000ff">$</FONT></TT><I>VAR</I> is returned with the first occurrence of the string <I>search</I>
replaced with <I>replace</I>.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">/#</FONT></TT><I>search</I><TT>
<FONT COLOR="#0000ff">/</FONT></TT><I>replace</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT></STRONG></DT>
<DD>Same as <TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">/</FONT></TT><I>search</I><TT>
<FONT COLOR="#0000ff">/</FONT></TT><I>replace</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT>
except that the match is attempted from the leading part of <TT>
<FONT COLOR="#0000ff">$</FONT></TT><I>VAR</I>.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">/%</FONT></TT><I>search</I><TT>
<FONT COLOR="#0000ff">/</FONT></TT><I>replace</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT></STRONG></DT>
<DD>Same as <TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">/</FONT></TT><I>search</I><TT>
<FONT COLOR="#0000ff">/</FONT></TT><I>replace</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT>
except that the match is attempted at the trailing part of <TT>
<FONT COLOR="#0000ff">$</FONT></TT><I>VAR</I>.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">//</FONT></TT><I>search</I><TT>
<FONT COLOR="#0000ff">/</FONT></TT><I>replace</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT></STRONG></DT>
<DD>Same as <TT>
<FONT COLOR="#0000ff">${</FONT></TT><I>VAR</I><TT>
<FONT COLOR="#0000ff">/</FONT></TT><I>search</I><TT>
<FONT COLOR="#0000ff">/</FONT></TT><I>replace</I><TT>
<FONT COLOR="#0000ff">}</FONT></TT>
except that all instances of <I>search</I> are replaced.
</DD>
</DL>
</DD>
<DT><STRONG><I>Backquote expansion</I></STRONG></DT>
<DD>We have already shown backquote expansion
in <A HREF="node10.html#sec:backquote">7.12</A>. Note that the additional notation
<TT>
<FONT COLOR="#0000ff">$(</FONT></TT><I>command</I>) is equivalent to <TT>
<FONT COLOR="#0000ff">`</FONT></TT><I>command</I>` except
that escapes (i.e., <TT>
<FONT COLOR="#0000ff">\</FONT></TT>) are not required for special characters.
</DD>
<DT><STRONG><I>Arithmetic expansion</I></STRONG></DT>
<DD>We have already shown arithmetic expansion
on page <A HREF="node10.html#page:arithshellbrace"><IMG ALIGN="BOTTOM" BORDER="1" ALT="[*]" SRC="crossref.png"></A>. Note that the additional notation
<TT>
<FONT COLOR="#0000ff">$((</FONT></TT><I>expression</I>)) is equivalent to <TT>
<FONT COLOR="#0000ff">$[</FONT></TT><I>expression</I>].
</DD>
<DT><STRONG><I>Finally</I></STRONG></DT>
<DD>The last modifications to the command-line are
the splitting of the command-line into words according to the white space
between them. The <TT>
<FONT COLOR="#0000ff">IFS</FONT></TT> (<I>Internal Field Separator</I>) environment
variable
determines what characters delimit command-line words (usually whitespace).
With the command-line divided into words, path names are expanded according
to glob wild cards. Consult <TT>
<FONT COLOR="#0000ff">bash</FONT></TT>(1) for a comprehensive
description of the pattern matching options that most people don't know
about.
</DD>
</DL>
<P>
<H1><A NAME="SECTION002340000000000000000">
20.4 Built-in Commands</A>
</H1>
<P>
Many commands operate some built-in functionality of <TT>
<FONT COLOR="#0000ff">bash</FONT></TT> or are
especially interpreted. These do not invoke an executable off the
file system. Some of these were described in Chapter
<A HREF="node10.html#chap:shellscript">7</A>, and a few more are discussed here. For an
exhaustive description, consult <TT>
<FONT COLOR="#0000ff">bash</FONT></TT>(1).
<DL>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">:</FONT></TT></STRONG></DT>
<DD>A single colon by itself does nothing. It is useful
for a ``no operation'' line such as:
</DD>
</DL>
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>if <command> ; then</code><br>
<code> :</code><br>
<code>else</code><br>
<code> echo "<command> was unsuccessful"</code><br>
<code>fi</code><br>
</FONT></TD></TR></TABLE><P>
<DL>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">.</FONT></TT> <I>filename args ...</I></STRONG></DT>
<DD>A single dot is the same as
the <TT>
<FONT COLOR="#0000ff">source</FONT></TT> command. See below.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">alias</FONT></TT> <I>command</I><TT>
<FONT COLOR="#0000ff">=</FONT></TT><I>value</I></STRONG></DT>
<DD>Creates a
pseudonym for a command. Try:
</DD>
</DL>
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>alias necho="echo -n"</code><br>
<code>necho "hello"</code><br>
</FONT></TD></TR></TABLE><P>
<DL>
<DT></DT>
<DD>Some distributions <TT>
<FONT COLOR="#0000ff">alias</FONT></TT> the <TT>
<FONT COLOR="#0000ff">mv</FONT></TT>, <TT>
<FONT COLOR="#0000ff">cp</FONT></TT>,
and <TT>
<FONT COLOR="#0000ff">rm</FONT></TT> commands to the same pseudonym with the <TT>
<FONT COLOR="#0000ff">-i</FONT></TT>
(<TT>
<FONT COLOR="#0000ff">i</FONT></TT>nteractive) option set. This prevents files from being deleted
without prompting, but can be irritating for the administrator.
See your <TT>
<FONT COLOR="#0000ff">~/.bashrc</FONT></TT> file for these settings. See also <TT>
<FONT COLOR="#0000ff">unalias</FONT></TT>.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">unalias</FONT></TT> <I>command</I></STRONG></DT>
<DD>Removes an alias created with <TT>
<FONT COLOR="#0000ff">alias</FONT></TT>.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">alias -p</FONT></TT></STRONG></DT>
<DD>Prints list of aliases.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">eval</FONT></TT> <I>arg ...</I></STRONG></DT>
<DD>Executes <I>arg</I>s as a line of shell script.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">exec</FONT></TT> <I>command arg ...</I></STRONG></DT>
<DD>Begins executing <I>command</I>
under the same process ID as the current script. This is most often
used for shell scripts that are mere ``wrapper'' scripts for
real programs. The wrapper script sets any environment variables
and then <TT>
<FONT COLOR="#0000ff">exec</FONT></TT>s the real program binary as its last line.
<TT>
<FONT COLOR="#0000ff">exec</FONT></TT> should never return.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">local</FONT></TT> <I>var</I><TT>
<FONT COLOR="#0000ff">=</FONT></TT><I>value</I></STRONG></DT>
<DD>Assigns a value
to a variable. The resulting variable is visible only within
the current function.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">pushd</FONT></TT> <I>directory</I> and <TT>
<FONT COLOR="#0000ff">popd</FONT></TT></STRONG></DT>
<DD>These two commands
are useful for jumping around directories. <TT>
<FONT COLOR="#0000ff">pushd</FONT></TT> can be used
instead of <TT>
<FONT COLOR="#0000ff">cd</FONT></TT>, but unlike <TT>
<FONT COLOR="#0000ff">cd</FONT></TT>, the directory is saved onto
a list of directories. At any time, entering <TT>
<FONT COLOR="#0000ff">popd</FONT></TT> returns you
to the previous directory. This is nice for navigation since it
keeps a history of wherever you have been.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">printf</FONT></TT> <I>format args ...</I></STRONG></DT>
<DD>This is like the <B>C</B> <TT>
<FONT COLOR="#0000ff">printf</FONT></TT>
function. It outputs to the terminal like <TT>
<FONT COLOR="#0000ff">echo</FONT></TT> but is useful
for more complex formatting of output. See <TT>
<FONT COLOR="#0000ff">printf</FONT></TT>(3) for details
and try <TT>
<FONT COLOR="#0000ff">printf "%10.3e\n" 12</FONT></TT> as an example.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">pwd</FONT></TT></STRONG></DT>
<DD>Prints the present working directory.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">set</FONT></TT></STRONG></DT>
<DD>Prints the value of all environment variables.
See also Section <A HREF="node23.html#sec:setbashbuiltin">20.6</A> on the <TT>
<FONT COLOR="#0000ff">set</FONT></TT> command.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">source</FONT></TT> <I>filename args ...</I></STRONG></DT>
<DD>Reads <I>filename</I>
into the current current shell environment. This is useful for
executing a shell script when environment variables set by
that script must be preserved.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">times</FONT></TT></STRONG></DT>
<DD>Prints the accumulated user and system times
for the shell and for processes run from the shell.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">type</FONT></TT> <I>command</I></STRONG></DT>
<DD>Tells whether <I>command</I>
is an alias, a built-in or a system executable.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">ulimit</FONT></TT></STRONG></DT>
<DD>Prints and sets various user resource limits
like memory usage limits and CPU limits. See <TT>
<FONT COLOR="#0000ff">bash</FONT></TT>(1) for details.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">umask</FONT></TT></STRONG></DT>
<DD>See Section <A HREF="node17.html#sec:umask">14.2</A>.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">unset</FONT></TT> <I>VAR</I></STRONG></DT>
<DD>Deletes a variable or environment variable.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">unset -f</FONT></TT> <I>func</I></STRONG></DT>
<DD>Deletes a function.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">wait</FONT></TT></STRONG></DT>
<DD>Pauses until all background jobs have completed.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">wait</FONT></TT> <I>PID</I></STRONG></DT>
<DD>Pauses until background process with
process ID of <I>PID</I> has exited, then returns the exit code
of the background process.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">wait %</FONT></TT><I>job</I></STRONG></DT>
<DD>Same with respect to a job spec.
</DD>
</DL>
<P>
<H1><A NAME="SECTION002350000000000000000">
20.5 Trapping Signals -- the <TT>
<FONT COLOR="#0000ff">trap</FONT></TT> Command</A>
</H1>
<P>
You will often want to make your script perform certain
actions in response to a signal. A list of signals can
be found on page <A HREF="node12.html#page:commonsignals"><IMG ALIGN="BOTTOM" BORDER="1" ALT="[*]" SRC="crossref.png"></A>. To trap
a signal, create a function and then use the <TT>
<FONT COLOR="#0000ff">trap</FONT></TT>
command to bind the function to the signal.
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#!/bin/sh</code><br>
<code> </code><br>
<code>function on_hangup ()</code><br>
<code>{</code><br>
<code> echo 'Hangup (SIGHUP) signal recieved'</code><br>
<code>}</code><br>
<code> </code><br>
<code>trap on_hangup SIGHUP</code><br>
<code> </code><br>
<code>while true ; do</code><br>
<code> sleep 1</code><br>
<code>done</code><br>
<code> </code><br>
<code>exit 0</code><br>
</FONT></TD></TR></TABLE><P>
Run the above script and then send the process ID the <TT>
<FONT COLOR="#0000ff">-HUP</FONT></TT>
signal to test it. (See Section <A HREF="node12.html#sec:killingproc">9.5</A>.)
<P>
An important function of a program is to clean up
after itself on exit. The special signal <TT>
<FONT COLOR="#0000ff">EXIT</FONT></TT> (not
really a signal) executes code on exit of the script:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#!/bin/sh</code><br>
<code> </code><br>
<code>function on_exit ()</code><br>
<code>{</code><br>
<code> echo 'I should remove temp files now'</code><br>
<code>}</code><br>
<code> </code><br>
<code>trap on_exit EXIT</code><br>
<code> </code><br>
<code>while true ; do</code><br>
<code> sleep 1</code><br>
<code>done</code><br>
<code> </code><br>
<code>exit 0</code><br>
</FONT></TD></TR></TABLE><P>
Breaking the above program will cause it to print its
own epitaph.
<P>
If <TT>
<FONT COLOR="#0000ff">-</FONT></TT> is given instead of a function name, then the signal
is unbound (i.e., set to its default value).
<P>
<H1><A NAME="SECTION002360000000000000000">
20.6 Internal Settings -- the <TT>
<FONT COLOR="#0000ff">set</FONT></TT> Command</A>
</H1>
<P>
<A NAME="sec:setbashbuiltin"></A>
<P>
The <TT>
<FONT COLOR="#0000ff">set</FONT></TT> command can modify certain behavioral
settings of the shell. Your current options can be displayed
with <TT>
<FONT COLOR="#0000ff">echo $-</FONT></TT>. Various <TT>
<FONT COLOR="#0000ff">set</FONT></TT> commands are
usually entered at the top of a script or given as command-line
options to <TT>
<FONT COLOR="#0000ff">bash</FONT></TT>. Using
<TT>
<FONT COLOR="#0000ff">set +</FONT></TT><I>option</I> instead of <TT>
<FONT COLOR="#0000ff">set -</FONT></TT><I>option</I>
disables the option. Here are a few examples:
<DL>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">set -e</FONT></TT></STRONG></DT>
<DD>Exit immediately if any simple command gives an error.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">set -h</FONT></TT></STRONG></DT>
<DD>Cache the location of commands in your <TT>
<FONT COLOR="#0000ff">PATH</FONT></TT>.
The shell will become confused if binaries are suddenly inserted into the directories
of your <TT>
<FONT COLOR="#0000ff">PATH</FONT></TT>, perhaps causing a <TT>
<FONT COLOR="#0000ff">No such file or directory</FONT></TT> error.
In this case, disable this option or restart your shell. This option is
enabled by default.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">set -n</FONT></TT></STRONG></DT>
<DD>Read commands without executing them. This command is useful
for syntax checking.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">set -o posix</FONT></TT></STRONG></DT>
<DD>Comply exactly with the POSIX 1003.2 standard.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">set -u</FONT></TT></STRONG></DT>
<DD>Report an error when trying to reference a variable that
is unset. Usually <TT>
<FONT COLOR="#0000ff">bash</FONT></TT> just fills in an empty string.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">set -v</FONT></TT></STRONG></DT>
<DD>Print each line of script as it is executed.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">set -x</FONT></TT></STRONG></DT>
<DD>Display each command expansion as it is executed.
</DD>
<DT><STRONG><TT>
<FONT COLOR="#0000ff">set -C</FONT></TT></STRONG></DT>
<DD>Do not overwrite existing files when using <TT>
<FONT COLOR="#0000ff">></FONT></TT>.
You can use <TT>
<FONT COLOR="#0000ff">>|</FONT></TT> to force overwriting.
</DD>
</DL>
<P>
<H1><A NAME="SECTION002370000000000000000">
20.7 Useful Scripts and Commands</A>
</H1>
<P>
Here is a collection of useful utility scripts that people
are always asking for on the mailing lists. See page
<A HREF="node47.html#page:knownrisksscripts"><IMG ALIGN="BOTTOM" BORDER="1" ALT="[*]" SRC="crossref.png"></A> for several security check
scripts.
<P>
<H2><A NAME="SECTION002371000000000000000">
20.7.1 <TT>
<FONT COLOR="#0000ff">chroot</FONT></TT></A>
</H2>
<P>
The <TT>
<FONT COLOR="#0000ff">chroot</FONT></TT> command makes a process think that its root
file system is not actually <TT>
<FONT COLOR="#0000ff">/</FONT></TT>. For example, on one system
I have a complete Debian installation residing under a directory,
say, <TT>
<FONT COLOR="#0000ff">/mnt/debian</FONT></TT>. I can issue the command
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>chroot /mnt/debian bash -i</code><br>
</FONT></TD></TR></TABLE><P>
to run the <TT>
<FONT COLOR="#0000ff">bash</FONT></TT> shell interactively, under the root
file system <TT>
<FONT COLOR="#0000ff">/mnt/debian</FONT></TT>. This command will hence run the command
<TT>
<FONT COLOR="#0000ff">/mnt/debian/bin/bash -i</FONT></TT>. All further commands processed under
this shell will have no knowledge of the real root directory,
so I can use my Debian installation without having to reboot.
All further commands will effectively behave as though they are
inside a separate U<SMALL>NIX</SMALL> machine. One caveat: you may have to
remount your <TT>
<FONT COLOR="#0000ff">/proc</FONT></TT> file system inside your <TT>
<FONT COLOR="#0000ff">chroot</FONT></TT>'d
file system--see page <A HREF="node22.html#page:mountproc"><IMG ALIGN="BOTTOM" BORDER="1" ALT="[*]" SRC="crossref.png"></A>.<A NAME="page:useingchroot"></A>
<P>
This useful for improving security. Insecure network services
can change to a different root directory--any corruption will not
affect the real system.
<P>
Most rescue disks have a <TT>
<FONT COLOR="#0000ff">chroot</FONT></TT> command. After booting
the disk, you can manually mount the file systems on your hard drive,
and then issue a <TT>
<FONT COLOR="#0000ff">chroot</FONT></TT> to begin using your machine as usual.
Note that the command <TT>
<FONT COLOR="#0000ff">chroot <new-root></FONT></TT> without arguments
invokes a shell by default.
<P>
<H2><A NAME="SECTION002372000000000000000">
20.7.2 <TT>
<FONT COLOR="#0000ff">if</FONT></TT> conditionals</A>
</H2>
<P>
The <TT>
<FONT COLOR="#0000ff">if test </FONT></TT><I>...</I> was used to control program flow
in Chapter <A HREF="node10.html#chap:shellscript">7</A>. Bash, however, has a built-in
alias for the <TT>
<FONT COLOR="#0000ff">test</FONT></TT> function: the left square brace, <TT>
<FONT COLOR="#0000ff">[</FONT></TT>.
<P>
Using <TT>
<FONT COLOR="#0000ff">[</FONT></TT> instead of <TT>
<FONT COLOR="#0000ff">test</FONT></TT> adds only elegance:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>if [ 5 -le 3 ] ; then</code><br>
<code> echo '5 < 3'</code><br>
<code>fi</code><br>
</FONT></TD></TR></TABLE><P>
<P>
It is important at this point to realize that the <TT>
<FONT COLOR="#0000ff">if</FONT></TT>
command understands nothing of arithmetic. It merely executes a command
<TT>
<FONT COLOR="#0000ff">test</FONT></TT> (or in this case <TT>
<FONT COLOR="#0000ff">[</FONT></TT>) and tests the exit code. If the
exit code is zero, then the command is considered to be successful and
<TT>
<FONT COLOR="#0000ff">if</FONT></TT> proceeds with the body of the <TT>
<FONT COLOR="#0000ff">if</FONT></TT> statement block. The
onus is on the <TT>
<FONT COLOR="#0000ff">test</FONT></TT> command to properly evaluate the expression
given to it.
<P>
<TT>
<FONT COLOR="#0000ff">if</FONT></TT> can equally well be used with any command:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>if echo "$PATH" | grep -qwv /usr/local/bin ; then</code><br>
<code> export PATH="$PATH:/usr/local/bin"</code><br>
<code>fi</code><br>
</FONT></TD></TR></TABLE><P>
conditionally adds <TT>
<FONT COLOR="#0000ff">/usr/local/bin</FONT></TT> if <TT>
<FONT COLOR="#0000ff">grep</FONT></TT>
does not find it in your <TT>
<FONT COLOR="#0000ff">PATH</FONT></TT>.
<P>
<H2><A NAME="SECTION002373000000000000000">
20.7.3 <TT>
<FONT COLOR="#0000ff">patch</FONT></TT>ing and <TT>
<FONT COLOR="#0000ff">diff</FONT></TT>ing</A>
</H2>
<P>
<A NAME="sec:applyingpatches"></A>
<P>
You may often want to find the differences between two files, for example to
see what changes have been made to a file between versions. Or, when a large
batch of source code may have been updated, it is silly to download the
entire directory tree if there have been only a few small changes. You
would want a list of alterations instead.
<P>
The <TT>
<FONT COLOR="#0000ff">diff</FONT></TT> utility dumps the lines that differ between two files. It can
be used as follows:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>diff -u <old-file> <new-file></code><br>
</FONT></TD></TR></TABLE><P>
You can also use <TT>
<FONT COLOR="#0000ff">diff</FONT></TT> to see difference netween two directory trees.
<TT>
<FONT COLOR="#0000ff">diff</FONT></TT> recursively compares all corresponding files:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>diff -u --recursive --new-file <old-dir> <new-dir> > <patch-file>.diff</code><br>
</FONT></TD></TR></TABLE><P>
The output is known as a <I>patch file</I> against a directory tree, that
can be used both to see changes, and to bring <TT>
<FONT COLOR="#0000ff"><old-dir></FONT></TT> up to date
with <TT>
<FONT COLOR="#0000ff"><new-dir></FONT></TT>.
<P>
Patch files may also end in <TT>
<FONT COLOR="#0000ff">.patch</FONT></TT> and are often <TT>
<FONT COLOR="#0000ff">gzip</FONT></TT>ped.
The patch file can be applied to <TT>
<FONT COLOR="#0000ff"><old-dir></FONT></TT> with
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>cd <old-dir></code><br>
<code>patch -p1 -s < <patch-file>.diff</code><br>
</FONT></TD></TR></TABLE><P>
which makes <TT>
<FONT COLOR="#0000ff"><old-dir></FONT></TT> identical to <TT>
<FONT COLOR="#0000ff"><new-dir></FONT></TT>. The <TT>
<FONT COLOR="#0000ff">-p1</FONT></TT>
option strips the leading directory name from the patch file. The presence of a leading
directory name in the patch file often confuses the <TT>
<FONT COLOR="#0000ff">patch</FONT></TT> command.
<P>
<H2><A NAME="SECTION002374000000000000000">
20.7.4 Internet connectivity test</A>
</H2>
<P>
<A NAME="sec:internconntest"></A>
<P>
You may want to leave this example until you have covered
more networking theory.
<P>
The acid test for an Internet connection is a successful
DNS query. You can use <TT>
<FONT COLOR="#0000ff">ping</FONT></TT> to test whether a server
is up, but some networks filter ICMP messages and <TT>
<FONT COLOR="#0000ff">ping</FONT></TT> does
not check that your DNS is working. <TT>
<FONT COLOR="#0000ff">dig</FONT></TT> sends a
single UDP packet similar to <TT>
<FONT COLOR="#0000ff">ping</FONT></TT>. Unfortunately, it takes
rather long to time out, so we fudge in a <TT>
<FONT COLOR="#0000ff">kill</FONT></TT> after
2 seconds.
<P>
This script blocks until it successfully queries a remote name server.
Typically, the next few lines of following script would run
<TT>
<FONT COLOR="#0000ff">fetchmail</FONT></TT> and a mail server
queue flush, or possibly <TT>
<FONT COLOR="#0000ff">uucico</FONT></TT>.
Do set the name server IP to something appropriate like that of your
local ISP; and increase the 2 second time out if your name server
typically takes longer to respond.
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>MY_DNS_SERVER=197.22.201.154</code><br>
<code> </code><br>
<code>while true ; do</code><br>
<code> (</code><br>
<code> dig @$MY_DNS_SERVER netscape.com IN A &</code><br>
<code> DIG_PID=$!</code><br>
<code> { sleep 2 ; kill $DIG_PID ; } &</code><br>
<code> sleep 1</code><br>
<code> wait $DIG_PID</code><br>
<code> ) 2>/dev/null | grep -q '^[^;]*netscape.com' && break</code><br>
<code>done</code><br>
</FONT></TD></TR></TABLE><P>
<P>
<H2><A NAME="SECTION002375000000000000000">
20.7.5 Recursive <TT>
<FONT COLOR="#0000ff">grep</FONT></TT> (search)</A>
</H2>
<P>
<A NAME="sec:recursivegrepxargs"></A>
<P>
Recursively searching through a directory tree can be done easily
with the <TT>
<FONT COLOR="#0000ff">find</FONT></TT> and <TT>
<FONT COLOR="#0000ff">xargs</FONT></TT> commands. You should consult
both these man pages. The following command pipe searches through
the kernel source for anything about the ``pcnet'' Ethernet card,
printing also the line number:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>find /usr/src/linux -follow -type f | xargs grep -iHn pcnet</code><br>
</FONT></TD></TR></TABLE><P>
(You will notice how this command returns rather a lot of data. However,
going through it carefully can be quite instructive.)
<P>
Limiting a search to a certain file extension is just another common
use of this pipe sequence.
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>find /usr/src/linux -follow -type f -name '*.[ch]' | xargs grep -iHn pcnet</code><br>
</FONT></TD></TR></TABLE><P>
<P>
Note that new versions of <TT>
<FONT COLOR="#0000ff">grep</FONT></TT> also have a <TT>
<FONT COLOR="#0000ff">-r</FONT></TT>
option to recursively search through directories.
<P>
<H2><A NAME="SECTION002376000000000000000">
20.7.6 Recursive search and replace</A>
</H2>
<P>
Often you will want to perform a search-and-replace throughout all the files
in an entire source tree. A typical example is the changing of a function call name
throughout lots of <B>C</B> source. The following script is a must
for any <TT>
<FONT COLOR="#0000ff">/usr/local/bin/</FONT></TT>. Notice the way it recursively
calls itself.
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>25</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>30</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>35</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>40</code></font><code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#!/bin/sh</code><br>
<code> </code><br>
<code>N=`basename $0`</code><br>
<code> </code><br>
<code>if [ "$1" = "-v" ] ; then</code><br>
<code> VERBOSE="-v"</code><br>
<code> shift</code><br>
<code>fi</code><br>
<code> </code><br>
<code>if [ "$3" = "" -o "$1" = "-h" -o "$1" = "--help" ] ; then</code><br>
<code> echo "$N: Usage"</code><br>
<code> echo " $N [-h|--help] [-v] <regexp-search> \</code><br>
<code><regexp-replace> <glob-file>"</code><br>
<code> echo</code><br>
<code> exit 0</code><br>
<code>fi</code><br>
<code> </code><br>
<code>S="$1" ; shift ; R="$1" ; shift</code><br>
<code>T=$$replc</code><br>
<code> </code><br>
<code>if echo "$1" | grep -q / ; then</code><br>
<code> for i in "$@" ; do</code><br>
<code> SEARCH=`echo "$S" | sed 's,/,\\\\/,g'`</code><br>
<code> REPLACE=`echo "$R" | sed 's,/,\\\\/,g'`</code><br>
<code> cat $i | sed "s/$SEARCH/$REPLACE/g" > $T</code><br>
<code> D="$?"</code><br>
<code> if [ "$D" = "0" ] ; then</code><br>
<code> if diff -q $T $i >/dev/null ; then</code><br>
<code> :</code><br>
<code> else</code><br>
<code> if [ "$VERBOSE" = "-v" ] ; then</code><br>
<code> echo $i</code><br>
<code> fi</code><br>
<code> cat $T > $i</code><br>
<code> fi</code><br>
<code> rm -f $T</code><br>
<code> fi</code><br>
<code> done</code><br>
<code>else</code><br>
<code> find . -type f -name "$1" | xargs $0 $VERBOSE "$S" "$R"</code><br>
<code>fi</code><br>
</FONT></TD></TR></TABLE><P>
<P>
<H2><A NAME="SECTION002377000000000000000">
20.7.7 <TT>
<FONT COLOR="#0000ff">cut</FONT></TT> and <TT>
<FONT COLOR="#0000ff">awk</FONT></TT> -- manipulating text file fields</A>
</H2>
<P>
The <TT>
<FONT COLOR="#0000ff">cut</FONT></TT> command is useful for slicing files into fields; try
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>cut -d: -f1 /etc/passwd</code><br>
<code>cat /etc/passwd | cut -d: -f1</code><br>
</FONT></TD></TR></TABLE><P>
The <TT>
<FONT COLOR="#0000ff">awk</FONT></TT> program is an interpreter for a complete programming language
call AWK. A common use for <TT>
<FONT COLOR="#0000ff">awk</FONT></TT> is in field stripping. It is slightly more
flexible than <TT>
<FONT COLOR="#0000ff">cut</FONT></TT>--
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>cat /etc/passwd | awk -F : '{print $1}'</code><br>
</FONT></TD></TR></TABLE><P>
--especially where whitespace gets in the way,
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>ls -al | awk '{print $6 " " $7 " " $8}'</code><br>
<code>ls -al | awk '{print $5 " bytes"}'</code><br>
</FONT></TD></TR></TABLE><P>
which isolates the time and size of the file respectively.
<P>
Get your nonlocal IP addresses with:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>ifconfig | grep 'inet addr:' | fgrep -v '127.0.0.' | \</code><br>
<code> cut -d: -f2 | cut -d' ' -f1</code><br>
</FONT></TD></TR></TABLE><P>
<P>
Reverse an IP address with:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>echo 192.168.3.2 | awk -F . '{print $4 "." $3 "." $2 "." $1 }'</code><br>
</FONT></TD></TR></TABLE><P>
<P>
Print all common user names (i.e., users with UID values greater than 499 on RedHat and greater
than 999 on Debian):
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>awk -F: '$3 >= 500 {print $1}' /etc/passwd</code><br>
<code>( awk -F: '$3 >= 1000 {print $1}' /etc/passwd )</code><br>
</FONT></TD></TR></TABLE><P>
<P>
<H2><A NAME="SECTION002378000000000000000">
20.7.8 Calculations with <TT>
<FONT COLOR="#0000ff">bc</FONT></TT></A>
</H2>
<P>
Scripts can easily use <TT>
<FONT COLOR="#0000ff">bc</FONT></TT> to do calculations that <TT>
<FONT COLOR="#0000ff">expr</FONT></TT>
can't handle. For example, convert to decimal with
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>echo -e 'ibase=16;FFFF' | bc</code><br>
</FONT></TD></TR></TABLE><P>
to binary with
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>echo -e 'obase=2;12345' | bc</code><br>
</FONT></TD></TR></TABLE><P>
or work out the SIN of 45 degrees with
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>pi=`echo "scale=10; 4*a(1)" | bc -l`</code><br>
<code>echo "scale=10; s(45*$pi/180)" | bc -l</code><br>
</FONT></TD></TR></TABLE><P>
<P>
<H2><A NAME="SECTION002379000000000000000">
20.7.9 Conversion of graphics formats of many files</A>
</H2>
<P>
The <TT>
<FONT COLOR="#0000ff">convert</FONT></TT> program of the <I>ImageMagick</I> package is a command
many Windows users would love. It can easily be used to convert multiple
files from one format to another. Changing a file's extension can be done
with <TT>
<FONT COLOR="#0000ff">echo </FONT></TT><I>filename</I><TT>
<FONT COLOR="#0000ff"> | sed -e 's/\.</FONT></TT><I>old</I><TT>
<FONT COLOR="#0000ff">$/.</FONT></TT><I>new</I><TT>
<FONT COLOR="#0000ff">/'`</FONT></TT>.
The <TT>
<FONT COLOR="#0000ff">convert</FONT></TT> command does the rest:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>for i in *.pcx ; do</code><br>
<code> CMD="convert -quality 625 $i `echo $i | sed -e 's/\.pcx$/.png/'`"</code><br>
<code># Show the command-line to the user:</code><br>
<code> echo $CMD</code><br>
<code># Execute the command-line:</code><br>
<code> eval $CMD</code><br>
<code>done</code><br>
</FONT></TD></TR></TABLE><P>
Note that the search-and-replace expansion mechanism could also be used
to replace the extensions: <TT>
<FONT COLOR="#0000ff">${i/%.pcx/.png}</FONT></TT> produces the
desired result.
<P>
Incidentally, the above nicely compresses high-resolution <TT>
<FONT COLOR="#0000ff">pcx</FONT></TT>
files--possibly the output of a scanning operation, or a L<SUP><SMALL>A</SMALL></SUP>T<SMALL>E</SMALL>X compilation
into PostScript rendered with GhostScript
(i.e. <TT>
<FONT COLOR="#0000ff">gs -sDEVICE=pcx256 -sOutputFile='page%d.pcx' </FONT></TT><I>file</I>.ps).
<P>
<H2><A NAME="SECTION0023710000000000000000">
20.7.10 Securely erasing files</A>
</H2>
<P>
Removing a file with <TT>
<FONT COLOR="#0000ff">rm</FONT></TT> only unlinks the file name from the data.
The file blocks may still be on disk, and will only be reclaimed when the
file system reuses that data. To
erase a file proper, requires writing
random bytes into the disk blocks occupied by the file. The following
overwrites all the files in the current directory:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>for i in * ; do</code><br>
<code> dd if=/dev/urandom \</code><br>
<code> of="$i" \</code><br>
<code> bs=1024 \</code><br>
<code> count=`expr 1 + \</code><br>
<code> \`stat "$i" | grep 'Size:' | awk '{print $2}'\` \</code><br>
<code> / 1024`</code><br>
<code>done</code><br>
</FONT></TD></TR></TABLE><P>
You can then remove the files normally with <TT>
<FONT COLOR="#0000ff">rm</FONT></TT>.
<P>
<H2><A NAME="SECTION0023711000000000000000">
20.7.11 Persistent background processes</A>
</H2>
<P>
Consider trying to run a process, say, the <TT>
<FONT COLOR="#0000ff">rxvt</FONT></TT> terminal,
in the background. This can be done simply with:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>rxvt &</code><br>
</FONT></TD></TR></TABLE><P>
However, <TT>
<FONT COLOR="#0000ff">rxvt</FONT></TT> still has its output connected
to the shell and is a child process of the shell. When a
login shell exits, it may take its child processes
with it. <TT>
<FONT COLOR="#0000ff">rxvt</FONT></TT> may also die of its own accord from
trying to read or write to a terminal that does not exist
without the parent shell. Now try:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>{ rxvt >/dev/null 2>&1 </dev/null & } &</code><br>
</FONT></TD></TR></TABLE><P>
This technique is known as <I>forking twice</I>, and
<I>redirecting the terminal to dev null</I>. The shell
can know about its child processes but not about the
its ``grand child'' processes. We have hence create a daemon process
proper with the above command.
<P>
Now, it is easy to create a daemon process that restarts itself
if it happens to die. Although such functionality is best
accomplished within <B>C</B> (which you will get a taste of
in Chapter <A HREF="node25.html#chap:trivintroc">22</A>), you can make do with:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>{ { while true ; do rxvt ; done ; } >/dev/null 2>&1 </dev/null & } &</code><br>
</FONT></TD></TR></TABLE><P>
You will notice the effects of all these tricks
with:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>ps awwwxf</code><br>
</FONT></TD></TR></TABLE><P>
<P>
<H2><A NAME="SECTION0023712000000000000000">
20.7.12 Processing the process list</A>
</H2>
<P>
The following command uses the custom format option of <TT>
<FONT COLOR="#0000ff">ps</FONT></TT>
to print every conceivable attribute of a process:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>ps -awwwxo %cpu,%mem,alarm,args,blocked,bsdstart,bsdtime,c,caught,cmd,comm,\</code><br>
<code>command,cputime,drs,dsiz,egid,egroup,eip,esp,etime,euid,euser,f,fgid,fgroup,\</code><br>
<code>flag,flags,fname,fsgid,fsgroup,fsuid,fsuser,fuid,fuser,gid,group,ignored,\</code><br>
<code>intpri,lim,longtname,lstart,m_drs,m_trs,maj_flt,majflt,min_flt,minflt,ni,\</code><br>
<code>nice,nwchan,opri,pagein,pcpu,pending,pgid,pgrp,pid,pmem,ppid,pri,rgid,rgroup,\</code><br>
<code>rss,rssize,rsz,ruid,ruser,s,sess,session,sgi_p,sgi_rss,sgid,sgroup,sid,sig,\</code><br>
<code>sig_block,sig_catch,sig_ignore,sig_pend,sigcatch,sigignore,sigmask,stackp,\</code><br>
<code>start,start_stack,start_time,stat,state,stime,suid,suser,svgid,svgroup,svuid,\</code><br>
<code>svuser,sz,time,timeout,tmout,tname,tpgid,trs,trss,tsiz,tt,tty,tty4,tty8,ucomm,\</code><br>
<code>uid,uid_hack,uname,user,vsize,vsz,wchan</code><br>
</FONT></TD></TR></TABLE><P>
The output is best piped to a file and viewed with a nonwrapping text editor.
More interestingly, the <TT>
<FONT COLOR="#0000ff">awk</FONT></TT> command can print the process ID of a process
with
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>ps awwx | grep -w 'htt[p]d' | awk '{print $1}'</code><br>
</FONT></TD></TR></TABLE><P>
which prints all the processes having <TT>
<FONT COLOR="#0000ff">httpd</FONT></TT> in the command
name or command-line. This filter is useful for
killing <TT>
<FONT COLOR="#0000ff">netscape</FONT></TT> as follows:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>kill -9 `ps awx | grep 'netsc[a]pe' | awk '{print $1}'`</code><br>
</FONT></TD></TR></TABLE><P>
(Note that the <TT>
<FONT COLOR="#0000ff">[a]</FONT></TT> in the regular expression
prevents <TT>
<FONT COLOR="#0000ff">grep</FONT></TT> from
finding itself in the process list.)
<P>
Other useful <TT>
<FONT COLOR="#0000ff">ps</FONT></TT> variations are:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>ps awwxf</code><br>
<code>ps awwxl</code><br>
<code>ps awwxv</code><br>
<code>ps awwxu</code><br>
<code>ps awwxs</code><br>
</FONT></TD></TR></TABLE><P>
The <TT>
<FONT COLOR="#0000ff">f</FONT></TT> option is most useful for showing parent-child relationships.
It stands for <TT>
<FONT COLOR="#0000ff">f</FONT></TT>orest, and shows the full process tree. For example, here
I am running an <B>X</B> desktop with two windows:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>25</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code> PID TTY STAT TIME COMMAND</code><br>
<code> 1 ? S 0:05 init [5]</code><br>
<code> 2 ? SW 0:02 [kflushd]</code><br>
<code> 3 ? SW 0:02 [kupdate]</code><br>
<code> 4 ? SW 0:00 [kpiod]</code><br>
<code> 5 ? SW 0:01 [kswapd]</code><br>
<code> 6 ? SW< 0:00 [mdrecoveryd]</code><br>
<code> 262 ? S 0:02 syslogd -m 0</code><br>
<code> 272 ? S 0:00 klogd</code><br>
<code> 341 ? S 0:00 xinetd -reuse -pidfile /var/run/xinetd.pid</code><br>
<code> 447 ? S 0:00 crond</code><br>
<code> 480 ? S 0:02 xfs -droppriv -daemon</code><br>
<code> 506 tty1 S 0:00 /sbin/mingetty tty1</code><br>
<code> 507 tty2 S 0:00 /sbin/mingetty tty2</code><br>
<code> 508 tty3 S 0:00 /sbin/mingetty tty3</code><br>
<code> 509 ? S 0:00 /usr/bin/gdm -nodaemon</code><br>
<code> 514 ? S 7:04 _ /etc/X11/X -auth /var/gdm/:0.Xauth :0</code><br>
<code> 515 ? S 0:00 _ /usr/bin/gdm -nodaemon</code><br>
<code> 524 ? S 0:18 _ /opt/icewm/bin/icewm</code><br>
<code> 748 ? S 0:08 _ rxvt -bg black -cr green -fg whi</code><br>
<code> 749 pts/0 S 0:00 | _ bash</code><br>
<code> 5643 pts/0 S 0:09 | _ mc</code><br>
<code> 5645 pts/6 S 0:02 | _ bash -rcfile .bashrc</code><br>
<code>25292 pts/6 R 0:00 | _ ps awwxf</code><br>
<code>11780 ? S 0:16 _ /usr/lib/netscape/netscape-commu</code><br>
<code>11814 ? S 0:00 _ (dns helper)</code><br>
<code>15534 pts/6 S 3:12 cooledit -I /root/.cedit/projects/Rute</code><br>
<code>15535 pts/6 S 6:03 _ aspell -a -a</code><br>
</FONT></TD></TR></TABLE><P>
The <TT>
<FONT COLOR="#0000ff">u</FONT></TT> option shows the useful <TT>
<FONT COLOR="#0000ff">u</FONT></TT>ser format,
and the others show <TT>
<FONT COLOR="#0000ff">v</FONT></TT>irtual memory, <TT>
<FONT COLOR="#0000ff">s</FONT></TT>ignal and
<TT>
<FONT COLOR="#0000ff">l</FONT></TT>ong format.
<P>
<H1><A NAME="SECTION002380000000000000000">
20.8 Shell Initialization</A>
</H1>
<P>
<A NAME="sec:shellinitializ"></A>
<P>
Here I will briefly discuss what
initialization takes place after
logging in and how to modify it.
<P>
The interactive shell invoked after <TT>
<FONT COLOR="#0000ff">login</FONT></TT> will be the shell specified
in the last field of the user's entry in the <TT>
<FONT COLOR="#0000ff">/etc/passwd</FONT></TT> file.
The <TT>
<FONT COLOR="#0000ff">login</FONT></TT> program will invoke the shell after authenticating the
user, placing a <TT>
<FONT COLOR="#0000ff">-</FONT></TT> in front of the the command name, which
indicates to the shell that it is a <I>login shell</I>, meaning that it
reads and execute several scripts to initialize the environment. In the
case of <TT>
<FONT COLOR="#0000ff">bash</FONT></TT>, the files it reads are: <TT>
<FONT COLOR="#0000ff">/etc/profile</FONT></TT>,
<TT>
<FONT COLOR="#0000ff">~/.bash_profile</FONT></TT>,
<TT>
<FONT COLOR="#0000ff">~/.bash_login</FONT></TT> and
<TT>
<FONT COLOR="#0000ff">~/.profile</FONT></TT>, in
that order. In addition, an interactive shell that is not a login shell
also reads
<TT>
<FONT COLOR="#0000ff">~/.bashrc</FONT></TT>. Note that traditional <TT>
<FONT COLOR="#0000ff">sh</FONT></TT> shells only
read <TT>
<FONT COLOR="#0000ff">/etc/profile</FONT></TT> and <TT>
<FONT COLOR="#0000ff">~/.profile</FONT></TT>.
<P>
<H2><A NAME="SECTION002381000000000000000">
20.8.1 Customizing the <TT>
<FONT COLOR="#0000ff">PATH</FONT></TT> and <TT>
<FONT COLOR="#0000ff">LD_LIBRARY_PATH</FONT></TT></A>
</H2>
<P>
Administrators can customise things like the environment variables
by modifying these startup scripts. Consider the classic
case of an installation tree under <TT>
<FONT COLOR="#0000ff">/opt/</FONT></TT>. Often, a package like
<TT>
<FONT COLOR="#0000ff">/opt/staroffice/</FONT></TT> or <TT>
<FONT COLOR="#0000ff">/opt/oracle/</FONT></TT> will require the <TT>
<FONT COLOR="#0000ff">PATH</FONT></TT>
and <TT>
<FONT COLOR="#0000ff">LD_LIBRARY_PATH</FONT></TT> variables to be adjusted accordingly. In
the case of RedHat, a script,
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>for i in /opt/*/bin /usr/local/bin ; do</code><br>
<code> test -d $i || continue</code><br>
<code> echo $PATH | grep -wq "$i" && continue</code><br>
<code> PATH=$PATH:$i</code><br>
<code> export PATH</code><br>
<code>done</code><br>
<code> </code><br>
<code>if test `id -u` -eq 0 ; then</code><br>
<code> for i in /opt/*/sbin /usr/local/sbin ; do</code><br>
<code> test -d $i || continue</code><br>
<code> echo $PATH | grep -wq "$i" && continue</code><br>
<code> PATH=$PATH:$i</code><br>
<code> export PATH</code><br>
<code> done</code><br>
<code>fi</code><br>
<code> </code><br>
<code>for i in /opt/*/lib /usr/local/lib ; do</code><br>
<code> test -d $i || continue</code><br>
<code> echo $LD_LIBRARY_PATH | grep -wq "$i" && continue</code><br>
<code> LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$i</code><br>
<code> export LD_LIBRARY_PATH</code><br>
<code>done</code><br>
</FONT></TD></TR></TABLE><P>
can be placed as <TT>
<FONT COLOR="#0000ff">/etc/profile.d/my_local.sh</FONT></TT> with
execute permissions. This will take care of anything installed
under <TT>
<FONT COLOR="#0000ff">/opt/</FONT></TT> or <TT>
<FONT COLOR="#0000ff">/usr/local/</FONT></TT>. For Debian, the script can be
inserted directly into <TT>
<FONT COLOR="#0000ff">/etc/profile</FONT></TT>.
<P>
Page <A HREF="node26.html#page:ldlibpathvar"><IMG ALIGN="BOTTOM" BORDER="1" ALT="[*]" SRC="crossref.png"></A> of Section <A HREF="node26.html#page:ldlibpathvar">23.3</A>
contains details of exactly what <TT>
<FONT COLOR="#0000ff">LD_LIBRARY_PATH</FONT></TT> is.
<P>
(Unrelated, but you should also edit your <TT>
<FONT COLOR="#0000ff">/etc/man.config</FONT></TT> to
add <TT>
<FONT COLOR="#0000ff">man</FONT></TT> page paths that appear under all installation trees
under <TT>
<FONT COLOR="#0000ff">/opt/</FONT></TT>.)
<P>
<H1><A NAME="SECTION002390000000000000000">
20.9 File Locking</A>
</H1>
<P>
Often, one would like a process to have
<I>exclusive access</I> to a file. By this we mean that only one process can
access the file at any one time. Consider a mail folder: if two
processes were to write to the folder simultaneously, it could
become corrupted. We also sometimes want to ensure that a
program can never be run twice at the same time; this insurance
is another use for ``locking.''
<P>
In the case of a mail folder, if the file is being
written to, then <I>no</I> other process should try read it or
write to it: and we would like to create a <I>write lock</I> on
the file. However if the file is being read from, <I>no</I> other
process should try to write to it: and we would like to create a
<I>read lock</I> on the file. Write locks are sometimes called
<I>exclusive locks</I>; read locks are sometimes called
<I>shared locks</I>. Often, <I>exclusive locks</I> are preferred
for simplicity.
<P>
Locking can be implemented by simply creating a temporary file
to indicate to other processes to wait before trying some kind of
access. U<SMALL>NIX</SMALL> also has some more sophisticated builtin functions.
<P>
<H2><A NAME="SECTION002391000000000000000">
20.9.1 Locking a mailbox file</A>
</H2>
<P>
There are currently four methods of file locking. <FONT COLOR="#ffa500">[The
<TT>
<FONT COLOR="#0000ff">exim</FONT></TT> sources seem to indicate
thorough research in this area, so this is what I am going on.]</FONT>
<P>
<DL COMPACT>
<DT>1.</DT>
<DD>``dot lock'' file locking. Here, a temporary file
is created with the same name as the mail folder and the extension
<TT>
<FONT COLOR="#0000ff">.lock</FONT></TT> added. So long as this file exists, no program should
try to access the folder. This is an exclusive lock only. It is
easy to write a shell script to do this kind of file locking.
</DD>
<DT>2.</DT>
<DD>``MBX'' file locking. Similar to 1, but a temporary file
is created in <TT>
<FONT COLOR="#0000ff">/tmp</FONT></TT>. This is also an exclusive lock.
</DD>
<DT>3.</DT>
<DD><TT>
<FONT COLOR="#0000ff">fcntl</FONT></TT> locking. Databases require areas of a file
to be locked. <TT>
<FONT COLOR="#0000ff">fcntl</FONT></TT> is a system call to be used inside <B>C</B>
programs.
</DD>
<DT>4.</DT>
<DD><TT>
<FONT COLOR="#0000ff">flock</FONT></TT> file locking. Same as <TT>
<FONT COLOR="#0000ff">fcntl</FONT></TT>, but locks
whole files.
</DD>
</DL>
<P>
The following shell function does proper mailbox file locking.
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>25</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>30</code></font><code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>function my_lockfile ()</code><br>
<code>{</code><br>
<code> TEMPFILE="$1.$$"</code><br>
<code> LOCKFILE="$1.lock"</code><br>
<code> echo $$ > $TEMPFILE 2>/dev/null || {</code><br>
<code> echo "You don't have permission to access `dirname $TEMPFILE`"</code><br>
<code> return 1</code><br>
<code> }</code><br>
<code> ln $TEMPFILE $LOCKFILE 2>/dev/null && {</code><br>
<code> rm -f $TEMPFILE</code><br>
<code> return 0</code><br>
<code> }</code><br>
<code> STALE_PID=`< $LOCKFILE`</code><br>
<code> test "$STALE_PID" -gt "0" >/dev/null || {</code><br>
<code> return 1</code><br>
<code> }</code><br>
<code> kill -0 $STALE_PID 2>/dev/null && {</code><br>
<code> rm -f $TEMPFILE</code><br>
<code> return 1</code><br>
<code> }</code><br>
<code> rm $LOCKFILE 2>/dev/null && {</code><br>
<code> echo "Removed stale lock file of process $STALE_PID"</code><br>
<code> }</code><br>
<code> ln $TEMPFILE $LOCKFILE 2>/dev/null && {</code><br>
<code> rm -f $TEMPFILE</code><br>
<code> return 0</code><br>
<code> }</code><br>
<code> rm -f $TEMPFILE</code><br>
<code> return 1</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
(Note how instead of <TT>
<FONT COLOR="#0000ff">`cat $LOCKFILE`</FONT></TT>, we use
<TT>
<FONT COLOR="#0000ff">`< $LOCKFILE`</FONT></TT>, which is faster.)
<P>
You can include the above function in scripts that need to lock
any kind file. Use the function as follows:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code># wait for a lock</code><br>
<code>until my_lockfile /etc/passwd ; do</code><br>
<code> sleep 1</code><br>
<code>done</code><br>
<code> </code><br>
<code># The body of the program might go here</code><br>
<code># [...]</code><br>
<code> </code><br>
<code># Then to remove the lock,</code><br>
<code>rm -f /etc/passwd.lock</code><br>
</FONT></TD></TR></TABLE><P>
<P>
This script is of academic interest only
but has a couple of interesting features. Note how
the <TT>
<FONT COLOR="#0000ff">ln</FONT></TT> function is used to ensure ``exclusivity.'' <TT>
<FONT COLOR="#0000ff">ln</FONT></TT>
is one of the few U<SMALL>NIX</SMALL> functions that is <I>atomic</I>,
meaning that only one link of the same name can exist, and its
creation excludes the possibility that another program would
think that it had successfully created the same link. One might
naively expect that the program
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>function my_lockfile ()</code><br>
<code>{</code><br>
<code> LOCKFILE="$1.lock"</code><br>
<code> test -e $LOCKFILE && return 1</code><br>
<code> touch $LOCKFILE</code><br>
<code> return 0</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
is sufficient for file locking. However, consider if two programs,
running simultaneously, executed line 4 at the same time. <I>Both</I> would
think that the lock did not exist and proceed to line 5. Then both
would successfully create the lock file--not what you wanted.
<P>
The <TT>
<FONT COLOR="#0000ff">kill</FONT></TT> command is then useful for checking whether
a process is running. Sending the <TT>
<FONT COLOR="#0000ff">0</FONT></TT> signal does nothing to
the process, but the signal fails if the process does not exist. This
technique can be used to remove a lock of a process that died before
removing the lock itself: that is, a <I>stale</I> lock.
<P>
<H2><A NAME="SECTION002392000000000000000">
20.9.2 Locking over NFS</A>
</H2>
<P>
The preceding script does <I>not</I> work if your file system is
mounted over NFS (<I>network file system</I>--see
Chapter <A HREF="node31.html#chap:nfs">28</A>). This is obvious because the script relies on the
PID of the process, which is not visible across different
machines. Not so obvious is that the <TT>
<FONT COLOR="#0000ff">ln</FONT></TT> function does not
work exactly right over NFS--you need to <TT>
<FONT COLOR="#0000ff">stat</FONT></TT> the
file and actually check that the link count has increased to 2.
<P>
The commands <TT>
<FONT COLOR="#0000ff">lockfile</FONT></TT> (from the <TT>
<FONT COLOR="#0000ff">procmail</FONT></TT> package)
and <TT>
<FONT COLOR="#0000ff">mutt_dotlock</FONT></TT> (from the <TT>
<FONT COLOR="#0000ff">mutt</FONT></TT> email reader but
perhaps not distributed) do similar file locking. These commands,
however, but do not store the PID in the lock file. Hence it
is not possible to detect a stale lock file. For example, to search
your mailbox, you can run:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>lockfile /var/spool/mail/mary.lock</code><br>
<code>grep freddy /var/spool/mail/mary</code><br>
<code>rm -f /var/spool/mail/mary.lock</code><br>
</FONT></TD></TR></TABLE><P>
This sequence ensures that you are searching a clean mailbox
even if <TT>
<FONT COLOR="#0000ff">/var</FONT></TT> is a remote NFS share.
<P>
<H2><A NAME="SECTION002393000000000000000">
20.9.3 Directory versus file locking</A>
</H2>
<P>
File locking is a headache for the developer. The
problem with U<SMALL>NIX</SMALL> is that whereas we are intuitively thinking
about locking a <I>file</I>, what we really mean is locking a
<I>file name</I> within a directory. <I>File</I> locking
<I>per se</I> should only be used on perpetual files, such as
database files. For mailbox and <TT>
<FONT COLOR="#0000ff">passwd</FONT></TT> files we need
<I>directory locking</I> <FONT COLOR="#ffa500">[My own term.]</FONT>, meaning the
exclusive access of one process to a particular directory entry.
In my opinion, lack of such a feature is a serious deficiency in
U<SMALL>NIX</SMALL>, but because it will require kernel, NFS, and (possibly)
<B>C</B> library extensions, will probably not come into being any
time soon.
<P>
<H2><A NAME="SECTION002394000000000000000">
20.9.4 Locking inside <B>C</B> programs</A>
</H2>
<P>
This topic is certainly outside of the scope of this text, except
to say that you should consult the source code of reputable packages
rather than invent your own locking scheme.
<P>
<P>
<HR>
<!--Navigation Panel-->
<A NAME="tex2html2123"
HREF="node24.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A>
<A NAME="tex2html2119"
HREF="rute.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A>
<A NAME="tex2html2113"
HREF="node22.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A>
<A NAME="tex2html2121"
HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A>
<BR>
<B> Next:</B> <A NAME="tex2html2124"
HREF="node24.html">21. System Services and</A>
<B> Up:</B> <A NAME="tex2html2120"
HREF="rute.html">rute</A>
<B> Previous:</B> <A NAME="tex2html2114"
HREF="node22.html">19. Partitions, File Systems,</A>
  <B> <A NAME="tex2html2122"
HREF="node1.html">Contents</A></B>
<!--End of Navigation Panel-->
</BODY>
</HTML>
|