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 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Miscellaneous Commands</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
"><LINK
REL="HOME"
TITLE="Advanced Bash-Scripting Guide"
HREF="index.html"><LINK
REL="UP"
TITLE="External Filters, Programs and Commands"
HREF="external.html"><LINK
REL="PREVIOUS"
TITLE="Math Commands"
HREF="mathc.html"><LINK
REL="NEXT"
TITLE="System and Administrative Commands"
HREF="system.html"><META
HTTP-EQUIV="Content-Style-Type"
CONTENT="text/css"><LINK
REL="stylesheet"
HREF="common/kde-common.css"
TYPE="text/css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=iso-8859-1"><META
HTTP-EQUIV="Content-Language"
CONTENT="en"><LINK
REL="stylesheet"
HREF="common/kde-localised.css"
TYPE="text/css"
TITLE="KDE-English"><LINK
REL="stylesheet"
HREF="common/kde-default.css"
TYPE="text/css"
TITLE="KDE-Default"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#AA0000"
VLINK="#AA0055"
ALINK="#AA0000"
STYLE="font-family: sans-serif;"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="mathc.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 16. External Filters, Programs and Commands</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="system.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="EXTMISC"
></A
>16.9. Miscellaneous Commands</H1
><DIV
CLASS="VARIABLELIST"
><P
><B
><A
NAME="MISCCOMMANDLISTING1"
></A
>Command that fit in no
special category</B
></P
><DL
><DT
><A
NAME="JOTREF"
></A
><B
CLASS="COMMAND"
>jot</B
>, <A
NAME="SEQREF"
></A
><B
CLASS="COMMAND"
>seq</B
></DT
><DD
><P
>These utilities emit a sequence of integers, with a
user-selectable increment.</P
><P
>The default separator character between each integer is a
newline, but this can be changed with the <TT
CLASS="OPTION"
>-s</TT
>
option.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>seq 5</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>1
2
3
4
5</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>seq -s : 5</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>1:2:3:4:5</TT
>
</PRE
></TD
></TR
></TABLE
>
</P
><P
>Both <B
CLASS="COMMAND"
>jot</B
> and <B
CLASS="COMMAND"
>seq</B
>
come in handy in a <A
HREF="loops.html#FORLOOPREF1"
>for
loop</A
>.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX53"
></A
><P
><B
>Example 16-54. Using <I
CLASS="FIRSTTERM"
>seq</I
> to generate loop
arguments</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # Using "seq"
3
4 echo
5
6 for a in `seq 80` # or for a in $( seq 80 )
7 # Same as for a in 1 2 3 4 5 ... 80 (saves much typing!).
8 # May also use 'jot' (if present on system).
9 do
10 echo -n "$a "
11 done # 1 2 3 4 5 ... 80
12 # Example of using the output of a command to generate
13 # the [list] in a "for" loop.
14
15 echo; echo
16
17
18 COUNT=80 # Yes, 'seq' also accepts a replaceable parameter.
19
20 for a in `seq $COUNT` # or for a in $( seq $COUNT )
21 do
22 echo -n "$a "
23 done # 1 2 3 4 5 ... 80
24
25 echo; echo
26
27 BEGIN=75
28 END=80
29
30 for a in `seq $BEGIN $END`
31 # Giving "seq" two arguments starts the count at the first one,
32 #+ and continues until it reaches the second.
33 do
34 echo -n "$a "
35 done # 75 76 77 78 79 80
36
37 echo; echo
38
39 BEGIN=45
40 INTERVAL=5
41 END=80
42
43 for a in `seq $BEGIN $INTERVAL $END`
44 # Giving "seq" three arguments starts the count at the first one,
45 #+ uses the second for a step interval,
46 #+ and continues until it reaches the third.
47 do
48 echo -n "$a "
49 done # 45 50 55 60 65 70 75 80
50
51 echo; echo
52
53 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>A simpler example:</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 # Create a set of 10 files,
2 #+ named file.1, file.2 . . . file.10.
3 COUNT=10
4 PREFIX=file
5
6 for filename in `seq $COUNT`
7 do
8 touch $PREFIX.$filename
9 # Or, can do other operations,
10 #+ such as rm, grep, etc.
11 done</PRE
></TD
></TR
></TABLE
></P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="LETTERCOUNT"
></A
><P
><B
>Example 16-55. Letter Count"</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # letter-count.sh: Counting letter occurrences in a text file.
3 # Written by Stefano Palmeri.
4 # Used in ABS Guide with permission.
5 # Slightly modified by document author.
6
7 MINARGS=2 # Script requires at least two arguments.
8 E_BADARGS=65
9 FILE=$1
10
11 let LETTERS=$#-1 # How many letters specified (as command-line args).
12 # (Subtract 1 from number of command-line args.)
13
14
15 show_help(){
16 echo
17 echo Usage: `basename $0` file letters
18 echo Note: `basename $0` arguments are case sensitive.
19 echo Example: `basename $0` foobar.txt G n U L i N U x.
20 echo
21 }
22
23 # Checks number of arguments.
24 if [ $# -lt $MINARGS ]; then
25 echo
26 echo "Not enough arguments."
27 echo
28 show_help
29 exit $E_BADARGS
30 fi
31
32
33 # Checks if file exists.
34 if [ ! -f $FILE ]; then
35 echo "File \"$FILE\" does not exist."
36 exit $E_BADARGS
37 fi
38
39
40
41 # Counts letter occurrences .
42 for n in `seq $LETTERS`; do
43 shift
44 if [[ `echo -n "$1" | wc -c` -eq 1 ]]; then # Checks arg.
45 echo "$1" -\> `cat $FILE | tr -cd "$1" | wc -c` # Counting.
46 else
47 echo "$1 is not a single char."
48 fi
49 done
50
51 exit $?
52
53 # This script has exactly the same functionality as letter-count2.sh,
54 #+ but executes faster.
55 # Why?</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>Somewhat more capable than <I
CLASS="FIRSTTERM"
>seq</I
>,
<B
CLASS="COMMAND"
>jot</B
> is a classic UNIX
utility that is not normally included in a standard Linux
distro. However, the source <I
CLASS="FIRSTTERM"
>rpm</I
>
is available for download from the <A
HREF="http://www.mit.edu/afs/athena/system/rhlinux/athena-9.0/free/SRPMS/athena-jot-9.0-3.src.rpm"
TARGET="_top"
> MIT repository</A
>.</P
><P
><A
NAME="JOTRANDOM"
></A
></P
><P
>Unlike <I
CLASS="FIRSTTERM"
>seq</I
>, <B
CLASS="COMMAND"
>jot</B
> can
generate a sequence of random numbers, using the <TT
CLASS="OPTION"
>-r</TT
>
option.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>jot -r 3 999</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>1069
1272
1428</TT
></PRE
></TD
></TR
></TABLE
></P
></TD
></TR
></TABLE
></DIV
></DD
><DT
><A
NAME="GETOPTY"
></A
><B
CLASS="COMMAND"
>getopt</B
></DT
><DD
><P
>The <B
CLASS="COMMAND"
>getopt</B
> command
parses command-line options preceded by a <A
HREF="special-chars.html#DASHREF"
>dash</A
>. This external command
corresponds to the <A
HREF="internal.html#GETOPTSX"
>getopts</A
>
Bash builtin. Using <B
CLASS="COMMAND"
>getopt</B
> permits
handling long options by means of the <TT
CLASS="OPTION"
>-l</TT
>
flag, and this also allows parameter reshuffling.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX33A"
></A
><P
><B
>Example 16-56. Using <I
CLASS="FIRSTTERM"
>getopt</I
> to parse command-line
options</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # Using getopt
3
4 # Try the following when invoking this script:
5 # sh ex33a.sh -a
6 # sh ex33a.sh -abc
7 # sh ex33a.sh -a -b -c
8 # sh ex33a.sh -d
9 # sh ex33a.sh -dXYZ
10 # sh ex33a.sh -d XYZ
11 # sh ex33a.sh -abcd
12 # sh ex33a.sh -abcdZ
13 # sh ex33a.sh -z
14 # sh ex33a.sh a
15 # Explain the results of each of the above.
16
17 E_OPTERR=65
18
19 if [ "$#" -eq 0 ]
20 then # Script needs at least one command-line argument.
21 echo "Usage $0 -[options a,b,c]"
22 exit $E_OPTERR
23 fi
24
25 set -- `getopt "abcd:" "$@"`
26 # Sets positional parameters to command-line arguments.
27 # What happens if you use "$*" instead of "$@"?
28
29 while [ ! -z "$1" ]
30 do
31 case "$1" in
32 -a) echo "Option \"a\"";;
33 -b) echo "Option \"b\"";;
34 -c) echo "Option \"c\"";;
35 -d) echo "Option \"d\" $2";;
36 *) break;;
37 esac
38
39 shift
40 done
41
42 # It is usually better to use the 'getopts' builtin in a script.
43 # See "ex33.sh."
44
45 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>As <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>Peggy Russell</I
></SPAN
> points out:</P
><P
>It is often necessary to include an <A
HREF="internal.html#EVALREF"
>eval</A
> to correctly process
<A
HREF="special-chars.html#WHITESPACEREF"
>whitespace</A
> and
<I
CLASS="FIRSTTERM"
>quotes</I
>.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 args=$(getopt -o a:bc:d -- "$@")
2 eval set -- "$args"</PRE
></TD
></TR
></TABLE
></P
></TD
></TR
></TABLE
></DIV
><P
>See <A
HREF="manipulatingvars.html#GETOPTSIMPLE"
>Example 10-5</A
> for a simplified emulation
of <B
CLASS="COMMAND"
>getopt</B
>.</P
></DD
><DT
><A
NAME="RUNPARTSREF"
></A
><B
CLASS="COMMAND"
>run-parts</B
></DT
><DD
><P
>The <B
CLASS="COMMAND"
>run-parts</B
> command
<A
NAME="AEN14105"
HREF="#FTN.AEN14105"
>[1]</A
>
executes all the scripts in a target directory, sequentially
in ASCII-sorted filename order. Of course, the scripts
need to have execute permission.</P
><P
>The <A
HREF="system.html#CRONREF"
>cron</A
> <A
HREF="communications.html#DAEMONREF"
>daemon</A
> invokes
<B
CLASS="COMMAND"
>run-parts</B
> to run the scripts in
the <TT
CLASS="FILENAME"
>/etc/cron.*</TT
>
directories.</P
></DD
><DT
><A
NAME="YESREF"
></A
><B
CLASS="COMMAND"
>yes</B
></DT
><DD
><P
>In its default behavior the <B
CLASS="COMMAND"
>yes</B
>
command feeds a continuous string of the character
<TT
CLASS="COMPUTEROUTPUT"
>y</TT
> followed
by a line feed to <TT
CLASS="FILENAME"
>stdout</TT
>. A
<B
CLASS="KEYCAP"
>control</B
>-<B
CLASS="KEYCAP"
>C</B
>
terminates the run. A different output string
may be specified, as in <TT
CLASS="USERINPUT"
><B
>yes different
string</B
></TT
>, which would continually output
<TT
CLASS="COMPUTEROUTPUT"
>different string</TT
> to
<TT
CLASS="FILENAME"
>stdout</TT
>.</P
><P
>One might well ask the purpose of this. From the
command-line or in a script, the output of
<B
CLASS="COMMAND"
>yes</B
> can be redirected or piped into a
program expecting user input. In effect, this becomes a sort
of poor man's version of <I
CLASS="FIRSTTERM"
>expect</I
>.</P
><P
><TT
CLASS="USERINPUT"
><B
>yes | fsck /dev/hda1</B
></TT
> runs
<B
CLASS="COMMAND"
>fsck</B
> non-interactively (careful!).</P
><P
><TT
CLASS="USERINPUT"
><B
>yes | rm -r dirname</B
></TT
> has same effect as
<TT
CLASS="USERINPUT"
><B
>rm -rf dirname</B
></TT
> (careful!).</P
><DIV
CLASS="WARNING"
><TABLE
CLASS="WARNING"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/warning.png"
HSPACE="5"
ALT="Warning"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>Caution advised when piping
<I
CLASS="FIRSTTERM"
>yes</I
> to a potentially dangerous
system command, such as <A
HREF="system.html#FSCKREF"
>fsck</A
>
or <A
HREF="system.html#FDISKREF"
>fdisk</A
>. It might have
unintended consequences.</P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>The <I
CLASS="FIRSTTERM"
>yes</I
> command parses variables,
or more accurately, it echoes parsed variables.
For example:</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>yes $BASH_VERSION</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>3.1.17(1)-release
3.1.17(1)-release
3.1.17(1)-release
3.1.17(1)-release
3.1.17(1)-release
. . .</TT
>
</PRE
></TD
></TR
></TABLE
>
</P
><P
> This particular <SPAN
CLASS="QUOTE"
>"feature"</SPAN
> may be used
to create a <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>very large</I
></SPAN
> ASCII file on the fly:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>yes $PATH > huge_file.txt</B
></TT
>
<TT
CLASS="USERINPUT"
><B
>Ctl-C</B
></TT
>
</PRE
></TD
></TR
></TABLE
>
Hit <TT
CLASS="USERINPUT"
><B
>Ctl-C</B
></TT
> <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>very
quickly</I
></SPAN
>, or you just might get more than you
bargained for. . . .
</P
></TD
></TR
></TABLE
></DIV
><P
><A
NAME="YESEMU"
></A
>The <I
CLASS="FIRSTTERM"
>yes</I
>
command may be emulated in a very simple script <A
HREF="functions.html#FUNCTIONREF"
>function</A
>.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 yes ()
2 { # Trivial emulation of "yes" ...
3 local DEFAULT_TEXT="y"
4 while [ true ] # Endless loop.
5 do
6 if [ -z "$1" ]
7 then
8 echo "$DEFAULT_TEXT"
9 else # If argument ...
10 echo "$1" # ... expand and echo it.
11 fi
12 done # The only things missing are the
13 } #+ --help and --version options.</PRE
></TD
></TR
></TABLE
></P
></DD
><DT
><A
NAME="BANNERREF"
></A
><B
CLASS="COMMAND"
>banner</B
></DT
><DD
><P
>Prints arguments as a large vertical banner to
<TT
CLASS="FILENAME"
>stdout</TT
>, using an <A
HREF="special-chars.html#ASCIIDEF"
>ASCII</A
> character (default
'#'). This may be redirected to a printer for
hardcopy.</P
><P
>Note that <I
CLASS="FIRSTTERM"
>banner</I
> has been
dropped from many Linux distros, presumably because it
is no longer considered useful.</P
></DD
><DT
><A
NAME="PRINTENVREF"
></A
><B
CLASS="COMMAND"
>printenv</B
></DT
><DD
><P
>Show all the <A
HREF="othertypesv.html#ENVREF"
>environmental
variables</A
> set for a particular user.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>printenv | grep HOME</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>HOME=/home/bozo</TT
>
</PRE
></TD
></TR
></TABLE
>
</P
></DD
><DT
><A
NAME="LPREF"
></A
><B
CLASS="COMMAND"
>lp</B
></DT
><DD
><P
>The <B
CLASS="COMMAND"
>lp</B
> and <B
CLASS="COMMAND"
>lpr</B
>
commands send file(s) to the print queue, to be printed as
hard copy.
<A
NAME="AEN14214"
HREF="#FTN.AEN14214"
>[2]</A
>
These commands trace the origin of their names to the
line printers of another era.
<A
NAME="AEN14218"
HREF="#FTN.AEN14218"
>[3]</A
>
</P
><P
><TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>lp file1.txt</B
></TT
>
or <TT
CLASS="PROMPT"
>bash </TT
><TT
CLASS="USERINPUT"
><B
>lp
<file1.txt</B
></TT
></P
><P
>It is often useful to pipe the formatted output from
<B
CLASS="COMMAND"
>pr</B
> to <B
CLASS="COMMAND"
>lp</B
>.</P
><P
><TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>pr -options file1.txt | lp</B
></TT
>
</P
><P
>Formatting packages, such as <A
HREF="textproc.html#GROFFREF"
>groff</A
> and
<I
CLASS="FIRSTTERM"
>Ghostscript</I
> may send their output
directly to <B
CLASS="COMMAND"
>lp</B
>.</P
><P
><TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>groff -Tascii file.tr | lp</B
></TT
>
</P
><P
><TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>gs -options | lp file.ps</B
></TT
>
</P
><P
>Related commands are <B
CLASS="COMMAND"
>lpq</B
>, for viewing
the print queue, and <B
CLASS="COMMAND"
>lprm</B
>, for removing
jobs from the print queue.</P
></DD
><DT
><A
NAME="TEEREF"
></A
><B
CLASS="COMMAND"
>tee</B
></DT
><DD
><P
>[UNIX borrows an idea from the plumbing trade.]</P
><P
>This is a redirection operator, but with a difference. Like the
plumber's <I
CLASS="FIRSTTERM"
>tee,</I
> it permits <SPAN
CLASS="QUOTE"
>"siphoning
off"</SPAN
> <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>to a file </I
></SPAN
>the output of a command
or commands within a pipe, but without affecting the result. This is
useful for printing an ongoing process to a file or paper, perhaps to
keep track of it for debugging purposes.</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> (redirection)
|----> to file
|
==========================|====================
command ---> command ---> |tee ---> command ---> ---> output of pipe
===============================================
</PRE
></TD
></TR
></TABLE
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 cat listfile* | sort | tee check.file | uniq > result.file
2 # ^^^^^^^^^^^^^^ ^^^^
3
4 # The file "check.file" contains the concatenated sorted "listfiles,"
5 #+ before the duplicate lines are removed by 'uniq.'</PRE
></TD
></TR
></TABLE
></P
></DD
><DT
><A
NAME="MKFIFOREF"
></A
><B
CLASS="COMMAND"
>mkfifo</B
></DT
><DD
><P
><A
NAME="NAMEDPIPEREF"
></A
>This obscure command
creates a <I
CLASS="FIRSTTERM"
>named pipe</I
>, a temporary
<I
CLASS="FIRSTTERM"
>first-in-first-out buffer</I
> for
transferring data between processes.
<A
NAME="AEN14280"
HREF="#FTN.AEN14280"
>[4]</A
>
Typically, one process writes to the FIFO, and the other
reads from it. See <A
HREF="contributed-scripts.html#FIFO"
>Example A-14</A
>.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # This short script by Omair Eshkenazi.
3 # Used in ABS Guide with permission (thanks!).
4
5 mkfifo pipe1 # Yes, pipes can be given names.
6 mkfifo pipe2 # Hence the designation "named pipe."
7
8 (cut -d' ' -f1 | tr "a-z" "A-Z") >pipe2 <pipe1 &
9 ls -l | tr -s ' ' | cut -d' ' -f3,9- | tee pipe1 |
10 cut -d' ' -f2 | paste - pipe2
11
12 rm -f pipe1
13 rm -f pipe2
14
15 # No need to kill background processes when script terminates (why not?).
16
17 exit $?
18
19 Now, invoke the script and explain the output:
20 sh mkfifo-example.sh
21
22 4830.tar.gz BOZO
23 pipe1 BOZO
24 pipe2 BOZO
25 mkfifo-example.sh BOZO
26 Mixed.msg BOZO</PRE
></TD
></TR
></TABLE
>
</P
></DD
><DT
><A
NAME="PATHCHKREF"
></A
><B
CLASS="COMMAND"
>pathchk</B
></DT
><DD
><P
>This command checks the validity of a filename. If the
filename exceeds the maximum allowable length (255
characters) or one or more of the directories in
its path is not searchable, then an error message
results.</P
><P
>Unfortunately, <B
CLASS="COMMAND"
>pathchk</B
> does
not return a recognizable error code, and it is therefore
pretty much useless in a script. Consider instead the
<A
HREF="fto.html#RTIF"
>file test operators</A
>.</P
></DD
><DT
><A
NAME="DDREF"
></A
><B
CLASS="COMMAND"
>dd</B
></DT
><DD
><P
>Though this somewhat obscure and much feared
<B
CLASS="COMMAND"
>d</B
>ata <B
CLASS="COMMAND"
>d</B
>uplicator
command originated as a utility for exchanging
data on magnetic tapes between UNIX minicomputers
and IBM mainframes, it still has its uses.
The <B
CLASS="COMMAND"
>dd</B
> command simply copies a
file (or <TT
CLASS="FILENAME"
>stdin/stdout</TT
>), but with
conversions. <A
NAME="DDCONVERSIONS"
></A
>Possible conversions
include ASCII/EBCDIC,
<A
NAME="AEN14318"
HREF="#FTN.AEN14318"
>[5]</A
>
upper/lower case, swapping of byte pairs between input
and output, and skipping and/or truncating the head or
tail of the input file.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 # Converting a file to all uppercase:
2
3 dd if=$filename conv=ucase > $filename.uppercase
4 # lcase # For lower case conversion</PRE
></TD
></TR
></TABLE
>
</P
><P
><A
NAME="DDOPTIONS"
></A
></P
><P
>Some basic options to <B
CLASS="COMMAND"
>dd</B
> are:
<UL
><LI
><P
>if=INFILE</P
><P
>INFILE is the <I
CLASS="FIRSTTERM"
>source</I
>
file.</P
></LI
><LI
><P
>of=OUTFILE</P
><P
>OUTFILE is the <I
CLASS="FIRSTTERM"
>target</I
>
file, the file that will have the data written to it.</P
></LI
><LI
><P
>bs=BLOCKSIZE</P
><P
>This is the size of each block of data being read
and written, usually a power of 2.</P
></LI
><LI
><P
>skip=BLOCKS</P
><P
>How many blocks of data to skip in INFILE before
starting to copy. This is useful when the INFILE has
<SPAN
CLASS="QUOTE"
>"garbage"</SPAN
> or garbled data in its
header or when it is desirable to copy only a portion
of the INFILE.</P
></LI
><LI
><P
>seek=BLOCKS</P
><P
>How many blocks of data to skip in OUTFILE before
starting to copy, leaving blank data at beginning
of OUTFILE.</P
></LI
><LI
><P
>count=BLOCKS</P
><P
>Copy only this many blocks of data, rather than the
entire INFILE.</P
></LI
><LI
><P
>conv=CONVERSION</P
><P
>Type of conversion to be applied to INFILE data
before copying operation.</P
></LI
></UL
>
</P
><P
>A <TT
CLASS="USERINPUT"
><B
>dd --help</B
></TT
> lists all the
options this powerful utility takes.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="SELFCOPY"
></A
><P
><B
>Example 16-57. A script that copies itself</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # self-copy.sh
3
4 # This script copies itself.
5
6 file_subscript=copy
7
8 dd if=$0 of=$0.$file_subscript 2>/dev/null
9 # Suppress messages from dd: ^^^^^^^^^^^
10
11 exit $?
12
13 # A program whose only output is its own source code
14 #+ is called a "quine" per Willard Quine.
15 # Does this script qualify as a quine?</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EXERCISINGDD"
></A
><P
><B
>Example 16-58. Exercising <I
CLASS="FIRSTTERM"
>dd</I
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # exercising-dd.sh
3
4 # Script by Stephane Chazelas.
5 # Somewhat modified by ABS Guide author.
6
7 infile=$0 # This script.
8 outfile=log.txt # Output file left behind.
9 n=8
10 p=11
11
12 dd if=$infile of=$outfile bs=1 skip=$((n-1)) count=$((p-n+1)) 2> /dev/null
13 # Extracts characters n to p (8 to 11) from this script ("bash").
14
15 # ----------------------------------------------------------------
16
17 echo -n "hello vertical world" | dd cbs=1 conv=unblock 2> /dev/null
18 # Echoes "hello vertical world" vertically downward.
19 # Why? A newline follows each character dd emits.
20
21 exit $?</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
><A
NAME="DDKEYSTROKES"
></A
></P
><P
>To demonstrate just how versatile <B
CLASS="COMMAND"
>dd</B
> is,
let's use it to capture keystrokes.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="DDKEYPRESS"
></A
><P
><B
>Example 16-59. Capturing Keystrokes</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # dd-keypress.sh: Capture keystrokes without needing to press ENTER.
3
4
5 keypresses=4 # Number of keypresses to capture.
6
7
8 old_tty_setting=$(stty -g) # Save old terminal settings.
9
10 echo "Press $keypresses keys."
11 stty -icanon -echo # Disable canonical mode.
12 # Disable local echo.
13 keys=$(dd bs=1 count=$keypresses 2> /dev/null)
14 # 'dd' uses stdin, if "if" (input file) not specified.
15
16 stty "$old_tty_setting" # Restore old terminal settings.
17
18 echo "You pressed the \"$keys\" keys."
19
20 # Thanks, Stephane Chazelas, for showing the way.
21 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
><A
NAME="DDRANDOM"
></A
></P
><P
>The <B
CLASS="COMMAND"
>dd</B
> command can do random access on a
data stream.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 echo -n . | dd bs=1 seek=4 of=file conv=notrunc
2 # The "conv=notrunc" option means that the output file
3 #+ will not be truncated.
4
5 # Thanks, S.C.</PRE
></TD
></TR
></TABLE
>
</P
><P
><A
NAME="DDCOPY"
></A
></P
><P
>The <B
CLASS="COMMAND"
>dd</B
> command can copy raw data
and disk images to and from devices, such as floppies and
tape drives (<A
HREF="contributed-scripts.html#COPYCD"
>Example A-5</A
>). A common use is
creating boot floppies.</P
><P
> <TT
CLASS="USERINPUT"
><B
>dd if=kernel-image of=/dev/fd0H1440</B
></TT
>
</P
><P
>Similarly, <B
CLASS="COMMAND"
>dd</B
> can copy the entire
contents of a floppy, even one formatted with a
<SPAN
CLASS="QUOTE"
>"foreign"</SPAN
> OS, to the hard drive as an
image file.</P
><P
> <TT
CLASS="USERINPUT"
><B
>dd if=/dev/fd0 of=/home/bozo/projects/floppy.img</B
></TT
>
</P
><P
><A
NAME="BFS"
></A
>Likewise, <B
CLASS="COMMAND"
>dd</B
>
can create bootable flash drives and SD cards.</P
><P
><TT
CLASS="USERINPUT"
><B
>dd if=image.iso of=/dev/sdb</B
></TT
></P
><P
><A
NAME="RPSDCARD01"
></A
></P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="RPSDCARD"
></A
><P
><B
>Example 16-60. Preparing a bootable SD card for the
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>Raspberry Pi</I
></SPAN
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # rp.sdcard.sh
3 # Preparing an SD card with a bootable image for the Raspberry Pi.
4
5 # $1 = imagefile name
6 # $2 = sdcard (device file)
7 # Otherwise defaults to the defaults, see below.
8
9 DEFAULTbs=4M # Block size, 4 mb default.
10 DEFAULTif="2013-07-26-wheezy-raspbian.img" # Commonly used distro.
11 DEFAULTsdcard="/dev/mmcblk0" # May be different. Check!
12 ROOTUSER_NAME=root # Must run as root!
13 E_NOTROOT=81
14 E_NOIMAGE=82
15
16 username=$(id -nu) # Who is running this script?
17 if [ "$username" != "$ROOTUSER_NAME" ]
18 then
19 echo "This script must run as root or with root privileges."
20 exit $E_NOTROOT
21 fi
22
23 if [ -n "$1" ]
24 then
25 imagefile="$1"
26 else
27 imagefile="$DEFAULTif"
28 fi
29
30 if [ -n "$2" ]
31 then
32 sdcard="$2"
33 else
34 sdcard="$DEFAULTsdcard"
35 fi
36
37 if [ ! -e $imagefile ]
38 then
39 echo "Image file \"$imagefile\" not found!"
40 exit $E_NOIMAGE
41 fi
42
43 echo "Last chance to change your mind!"; echo
44 read -s -n1 -p "Hit a key to write $imagefile to $sdcard [Ctl-c to exit]."
45 echo; echo
46
47 echo "Writing $imagefile to $sdcard ..."
48 dd bs=$DEFAULTbs if=$imagefile of=$sdcard
49
50 exit $?
51
52 # Exercises:
53 # ---------
54 # 1) Provide additional error checking.
55 # 2) Have script autodetect device file for SD card (difficult!).
56 # 3) Have script sutodetect image file (*img) in $PWD.</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
><A
NAME="DDSWAP"
></A
></P
><P
> Other applications of <B
CLASS="COMMAND"
>dd</B
> include
initializing temporary swap files (<A
HREF="zeros.html#EX73"
>Example 31-2</A
>)
and ramdisks (<A
HREF="zeros.html#RAMDISK"
>Example 31-3</A
>). It can even do a
low-level copy of an entire hard drive partition, although
this is not necessarily recommended.</P
><P
>People (with presumably nothing better to do with
their time) are constantly thinking of interesting
applications of <B
CLASS="COMMAND"
>dd</B
>.</P
><P
><A
NAME="DDFDEL"
></A
></P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="BLOTOUT"
></A
><P
><B
>Example 16-61. Securely deleting a file</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # blot-out.sh: Erase "all" traces of a file.
3
4 # This script overwrites a target file alternately
5 #+ with random bytes, then zeros before finally deleting it.
6 # After that, even examining the raw disk sectors by conventional methods
7 #+ will not reveal the original file data.
8
9 PASSES=7 # Number of file-shredding passes.
10 # Increasing this slows script execution,
11 #+ especially on large target files.
12 BLOCKSIZE=1 # I/O with /dev/urandom requires unit block size,
13 #+ otherwise you get weird results.
14 E_BADARGS=70 # Various error exit codes.
15 E_NOT_FOUND=71
16 E_CHANGED_MIND=72
17
18 if [ -z "$1" ] # No filename specified.
19 then
20 echo "Usage: `basename $0` filename"
21 exit $E_BADARGS
22 fi
23
24 file=$1
25
26 if [ ! -e "$file" ]
27 then
28 echo "File \"$file\" not found."
29 exit $E_NOT_FOUND
30 fi
31
32 echo; echo -n "Are you absolutely sure you want to blot out \"$file\" (y/n)? "
33 read answer
34 case "$answer" in
35 [nN]) echo "Changed your mind, huh?"
36 exit $E_CHANGED_MIND
37 ;;
38 *) echo "Blotting out file \"$file\".";;
39 esac
40
41
42 flength=$(ls -l "$file" | awk '{print $5}') # Field 5 is file length.
43 pass_count=1
44
45 chmod u+w "$file" # Allow overwriting/deleting the file.
46
47 echo
48
49 while [ "$pass_count" -le "$PASSES" ]
50 do
51 echo "Pass #$pass_count"
52 sync # Flush buffers.
53 dd if=/dev/urandom of=$file bs=$BLOCKSIZE count=$flength
54 # Fill with random bytes.
55 sync # Flush buffers again.
56 dd if=/dev/zero of=$file bs=$BLOCKSIZE count=$flength
57 # Fill with zeros.
58 sync # Flush buffers yet again.
59 let "pass_count += 1"
60 echo
61 done
62
63
64 rm -f $file # Finally, delete scrambled and shredded file.
65 sync # Flush buffers a final time.
66
67 echo "File \"$file\" blotted out and deleted."; echo
68
69
70 exit 0
71
72 # This is a fairly secure, if inefficient and slow method
73 #+ of thoroughly "shredding" a file.
74 # The "shred" command, part of the GNU "fileutils" package,
75 #+ does the same thing, although more efficiently.
76
77 # The file cannot not be "undeleted" or retrieved by normal methods.
78 # However . . .
79 #+ this simple method would *not* likely withstand
80 #+ sophisticated forensic analysis.
81
82 # This script may not play well with a journaled file system.
83 # Exercise (difficult): Fix it so it does.
84
85
86
87 # Tom Vier's "wipe" file-deletion package does a much more thorough job
88 #+ of file shredding than this simple script.
89 # http://www.ibiblio.org/pub/Linux/utils/file/wipe-2.0.0.tar.bz2
90
91 # For an in-depth analysis on the topic of file deletion and security,
92 #+ see Peter Gutmann's paper,
93 #+ "Secure Deletion of Data From Magnetic and Solid-State Memory".
94 # http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>See also the <A
HREF="biblio.html#DDLINK"
>dd
thread</A
> entry in the <A
HREF="biblio.html#BIBLIOREF"
>bibliography</A
>.</P
></DD
><DT
><A
NAME="ODREF"
></A
><B
CLASS="COMMAND"
>od</B
></DT
><DD
><P
>The <B
CLASS="COMMAND"
>od</B
>, or <I
CLASS="FIRSTTERM"
>octal
dump</I
> filter converts input (or files) to octal
(base-8) or other bases. This is useful for viewing or
processing binary data files or otherwise unreadable system
<A
HREF="devproc.html#DEVFILEREF"
>device files</A
>, such as
<TT
CLASS="FILENAME"
>/dev/urandom</TT
>, and as a filter for
binary data.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p'
2 # Sample output: 1324725719, 3918166450, 2989231420, etc.
3
4 # From rnd.sh example script, by Stphane Chazelas</PRE
></TD
></TR
></TABLE
>
</P
><P
>See also <A
HREF="randomvar.html#SEEDINGRANDOM"
>Example 9-16</A
> and <A
HREF="contributed-scripts.html#INSERTIONSORT"
>Example A-36</A
>.</P
></DD
><DT
><A
NAME="HEXDUMPREF"
></A
><B
CLASS="COMMAND"
>hexdump</B
></DT
><DD
><P
>Performs a hexadecimal, octal, decimal, or ASCII
dump of a binary file. This command is the rough equivalent
of <B
CLASS="COMMAND"
>od</B
>, above, but not nearly as
useful. May be used to view the contents of a binary file,
in combination with <A
HREF="extmisc.html#DDREF"
>dd</A
> and <A
HREF="filearchiv.html#LESSREF"
>less</A
>.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 dd if=/bin/ls | hexdump -C | less
2 # The -C option nicely formats the output in tabular form.</PRE
></TD
></TR
></TABLE
>
</P
></DD
><DT
><A
NAME="OBJDUMPREF"
></A
><B
CLASS="COMMAND"
>objdump</B
></DT
><DD
><P
>Displays information about an object file or binary
executable in either hexadecimal form or as a disassembled
listing (with the <TT
CLASS="OPTION"
>-d</TT
> option).</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>objdump -d /bin/ls</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>/bin/ls: file format elf32-i386
Disassembly of section .init:
080490bc <.init>:
80490bc: 55 push %ebp
80490bd: 89 e5 mov %esp,%ebp
. . .</TT
>
</PRE
></TD
></TR
></TABLE
>
</P
></DD
><DT
><A
NAME="MCOOKIEREF"
></A
><B
CLASS="COMMAND"
>mcookie</B
></DT
><DD
><P
>This command generates a <SPAN
CLASS="QUOTE"
>"magic cookie,"</SPAN
> a
128-bit (32-character) pseudorandom hexadecimal number,
normally used as an authorization <SPAN
CLASS="QUOTE"
>"signature"</SPAN
>
by the X server. This also available for use in a script
as a <SPAN
CLASS="QUOTE"
>"quick 'n dirty"</SPAN
> random number.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 random000=$(mcookie)</PRE
></TD
></TR
></TABLE
></P
><P
>Of course, a script could use <A
HREF="filearchiv.html#MD5SUMREF"
>md5sum</A
> for the same purpose.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 # Generate md5 checksum on the script itself.
2 random001=`md5sum $0 | awk '{print $1}'`
3 # Uses 'awk' to strip off the filename.</PRE
></TD
></TR
></TABLE
></P
><P
>The <B
CLASS="COMMAND"
>mcookie</B
> command gives yet another way
to generate a <SPAN
CLASS="QUOTE"
>"unique"</SPAN
> filename.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="TEMPFILENAME"
></A
><P
><B
>Example 16-62. Filename generator</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # tempfile-name.sh: temp filename generator
3
4 BASE_STR=`mcookie` # 32-character magic cookie.
5 POS=11 # Arbitrary position in magic cookie string.
6 LEN=5 # Get $LEN consecutive characters.
7
8 prefix=temp # This is, after all, a "temp" file.
9 # For more "uniqueness," generate the
10 #+ filename prefix using the same method
11 #+ as the suffix, below.
12
13 suffix=${BASE_STR:POS:LEN}
14 # Extract a 5-character string,
15 #+ starting at position 11.
16
17 temp_filename=$prefix.$suffix
18 # Construct the filename.
19
20 echo "Temp filename = "$temp_filename""
21
22 # sh tempfile-name.sh
23 # Temp filename = temp.e19ea
24
25 # Compare this method of generating "unique" filenames
26 #+ with the 'date' method in ex51.sh.
27
28 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
></DD
><DT
><A
NAME="UNITSREF"
></A
><B
CLASS="COMMAND"
>units</B
></DT
><DD
><P
>This utility converts between different <I
CLASS="FIRSTTERM"
>units
of measure</I
>. While normally invoked in interactive
mode, <B
CLASS="COMMAND"
>units</B
> may find use in a
script.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="UNITCONVERSION"
></A
><P
><B
>Example 16-63. Converting meters to miles</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # unit-conversion.sh
3 # Must have 'units' utility installed.
4
5
6 convert_units () # Takes as arguments the units to convert.
7 {
8 cf=$(units "$1" "$2" | sed --silent -e '1p' | awk '{print $2}')
9 # Strip off everything except the actual conversion factor.
10 echo "$cf"
11 }
12
13 Unit1=miles
14 Unit2=meters
15 cfactor=`convert_units $Unit1 $Unit2`
16 quantity=3.73
17
18 result=$(echo $quantity*$cfactor | bc)
19
20 echo "There are $result $Unit2 in $quantity $Unit1."
21
22 # What happens if you pass incompatible units,
23 #+ such as "acres" and "miles" to the function?
24
25 exit 0
26
27 # Exercise: Edit this script to accept command-line parameters,
28 # with appropriate error checking, of course.</PRE
></TD
></TR
></TABLE
><HR></DIV
></DD
><DT
><A
NAME="M4REF"
></A
><B
CLASS="COMMAND"
>m4</B
></DT
><DD
><P
>A hidden treasure, <B
CLASS="COMMAND"
>m4</B
> is a
powerful macro
<A
NAME="AEN14523"
HREF="#FTN.AEN14523"
>[6]</A
>
processing filter, virtually a complete language.
Although originally written as a pre-processor
for <I
CLASS="FIRSTTERM"
>RatFor</I
>, <B
CLASS="COMMAND"
>m4</B
>
turned out to be useful as a stand-alone utility. In
fact, <B
CLASS="COMMAND"
>m4</B
> combines some of the
functionality of <A
HREF="internal.html#EVALREF"
>eval</A
>,
<A
HREF="textproc.html#TRREF"
>tr</A
>, and <A
HREF="awk.html#AWKREF"
>awk</A
>, in addition to its extensive
macro expansion facilities.</P
><P
>The April, 2002 issue of <A
HREF="http://www.linuxjournal.com"
TARGET="_top"
><I
CLASS="CITETITLE"
>Linux Journal</I
></A
>
has a very nice article on <B
CLASS="COMMAND"
>m4</B
> and
its uses.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="M4"
></A
><P
><B
>Example 16-64. Using <I
CLASS="FIRSTTERM"
>m4</I
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # m4.sh: Using the m4 macro processor
3
4 # Strings
5 string=abcdA01
6 echo "len($string)" | m4 # 7
7 echo "substr($string,4)" | m4 # A01
8 echo "regexp($string,[0-1][0-1],\&Z)" | m4 # 01Z
9
10 # Arithmetic
11 var=99
12 echo "incr($var)" | m4 # 100
13 echo "eval($var / 3)" | m4 # 33
14
15 exit</PRE
></TD
></TR
></TABLE
><HR></DIV
></DD
><DT
><A
NAME="XMESSAGEREF"
></A
><B
CLASS="COMMAND"
>xmessage</B
></DT
><DD
><P
>This X-based variant of
<A
HREF="internal.html#ECHOREF"
>echo</A
> pops up a message/query
window on the desktop.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 xmessage Left click to continue -button okay</PRE
></TD
></TR
></TABLE
>
</P
></DD
><DT
><A
NAME="ZENITYREF"
></A
><B
CLASS="COMMAND"
>zenity</B
></DT
><DD
><P
>The
<A
HREF="http://freshmeat.net/projects/zenity"
TARGET="_top"
>zenity</A
>
utility is adept at displaying
<I
CLASS="FIRSTTERM"
>GTK+</I
> dialog <A
HREF="assortedtips.html#WIDGETREF"
>widgets</A
> and <A
HREF="assortedtips.html#ZENITYREF2"
>very suitable for scripting
purposes</A
>.</P
></DD
><DT
><A
NAME="DOEXECREF"
></A
><B
CLASS="COMMAND"
>doexec</B
></DT
><DD
><P
>The <B
CLASS="COMMAND"
>doexec</B
> command enables passing
an arbitrary list of arguments to a <I
CLASS="FIRSTTERM"
>binary
executable</I
>. In particular, passing
<TT
CLASS="PARAMETER"
><I
>argv[0]</I
></TT
> (which corresponds to <A
HREF="othertypesv.html#POSPARAMREF1"
>$0</A
> in a script) lets the
executable be invoked by various names, and it can then
carry out different sets of actions, according to the name
by which it was called. What this amounts to is roundabout
way of passing options to an executable.</P
><P
>For example, the <TT
CLASS="FILENAME"
>/usr/local/bin</TT
> directory might
contain a binary called <SPAN
CLASS="QUOTE"
>"aaa"</SPAN
>. Invoking
<B
CLASS="COMMAND"
>doexec /usr/local/bin/aaa list</B
>
would <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>list</I
></SPAN
> all those files
in the current working directory beginning with an
<SPAN
CLASS="QUOTE"
>"a"</SPAN
>, while invoking (the same executable
with) <B
CLASS="COMMAND"
>doexec /usr/local/bin/aaa delete </B
>
would <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>delete</I
></SPAN
> those files.</P
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>The various behaviors of the executable
must be defined within the code of the executable itself,
analogous to something like the following in a shell script:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 case `basename $0` in
2 "name1" ) do_something;;
3 "name2" ) do_something_else;;
4 "name3" ) do_yet_another_thing;;
5 * ) bail_out;;
6 esac</PRE
></TD
></TR
></TABLE
></P
></TD
></TR
></TABLE
></DIV
></DD
><DT
><B
CLASS="COMMAND"
>dialog</B
></DT
><DD
><P
>The <A
HREF="assortedtips.html#DIALOGREF"
>dialog</A
> family of tools
provide a method of calling interactive
<SPAN
CLASS="QUOTE"
>"dialog"</SPAN
> boxes from a script. The more
elaborate variations of <B
CLASS="COMMAND"
>dialog</B
> --
<B
CLASS="COMMAND"
>gdialog</B
>, <B
CLASS="COMMAND"
>Xdialog</B
>,
and <B
CLASS="COMMAND"
>kdialog</B
> -- actually invoke X-Windows
<A
HREF="assortedtips.html#WIDGETREF"
>widgets</A
>.</P
></DD
><DT
><A
NAME="SOXREF"
></A
><B
CLASS="COMMAND"
>sox</B
></DT
><DD
><P
>The <B
CLASS="COMMAND"
>sox</B
>, or
<SPAN
CLASS="QUOTE"
>"<B
CLASS="COMMAND"
>so</B
>und
e<B
CLASS="COMMAND"
>x</B
>change"</SPAN
> command plays and
performs transformations on sound files. In fact,
the <TT
CLASS="FILENAME"
>/usr/bin/play</TT
> executable
(now deprecated) is nothing but a shell wrapper for
<I
CLASS="FIRSTTERM"
>sox</I
>.</P
><P
>For example, <B
CLASS="COMMAND"
>sox soundfile.wav
soundfile.au</B
> changes a WAV sound file into a
(Sun audio format) AU sound file.</P
><P
>Shell scripts are ideally suited for batch-processing
<B
CLASS="COMMAND"
>sox</B
> operations on
sound files. For examples, see the <A
HREF="http://osl.iu.edu/~tveldhui/radio/"
TARGET="_top"
> Linux Radio
Timeshift HOWTO</A
> and the <A
HREF="http://savannah.nongnu.org/projects/audiodo"
TARGET="_top"
>MP3do
Project</A
>.</P
></DD
></DL
></DIV
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN14105"
HREF="extmisc.html#AEN14105"
>[1]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>This is actually a script adapted from
the Debian Linux distribution.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN14214"
HREF="extmisc.html#AEN14214"
>[2]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>The <I
CLASS="FIRSTTERM"
>print queue</I
> is
the group of jobs <SPAN
CLASS="QUOTE"
>"waiting in line"</SPAN
> to be
printed.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN14218"
HREF="extmisc.html#AEN14218"
>[3]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>Large mechanical <I
CLASS="FIRSTTERM"
>line
printers</I
> printed a single
line of type at a time onto joined
sheets of <I
CLASS="FIRSTTERM"
>greenbar</I
>
paper, to the accompaniment of <A
HREF="http://www.columbia.edu/cu/computinghistory/1403.html"
TARGET="_top"
>a
great deal of noise</A
>. The hardcopy
thusly printed was referred to as a
<I
CLASS="FIRSTTERM"
>printout</I
>.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN14280"
HREF="extmisc.html#AEN14280"
>[4]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>For an excellent overview of this
topic, see Andy Vaught's article, <A
HREF="http://www2.linuxjournal.com/lj-issues/issue41/2156.html"
TARGET="_top"
>Introduction
to Named Pipes</A
>, in the September, 1997 issue of
<A
HREF="http://www.linuxjournal.com"
TARGET="_top"
><I
CLASS="CITETITLE"
>Linux
Journal</I
></A
>.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN14318"
HREF="extmisc.html#AEN14318"
>[5]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
><SPAN
CLASS="ACRONYM"
>EBCDIC</SPAN
> (pronounced
<SPAN
CLASS="QUOTE"
>"ebb-sid-ick"</SPAN
>) is an acronym for Extended
Binary Coded Decimal Interchange Code, an obsolete
IBM data format. A bizarre application of
the <TT
CLASS="OPTION"
>conv=ebcdic</TT
> option of
<B
CLASS="COMMAND"
>dd</B
> is as a quick 'n easy, but not
very secure text file encoder.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 cat $file | dd conv=swab,ebcdic > $file_encrypted
2 # Encode (looks like gibberish).
3 # Might as well switch bytes (swab), too, for a little extra obscurity.
4
5 cat $file_encrypted | dd conv=swab,ascii > $file_plaintext
6 # Decode.</PRE
></TD
></TR
></TABLE
>
</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN14523"
HREF="extmisc.html#AEN14523"
>[6]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>A <I
CLASS="FIRSTTERM"
>macro</I
> is a
symbolic constant that expands into a command string
or a set of operations on parameters. Simply put,
it's a shortcut or abbreviation.</P
></TD
></TR
></TABLE
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="mathc.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="system.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Math Commands</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="external.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>System and Administrative Commands</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|