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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Shell Programming!</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="Introduction"
HREF="part1.html"><LINK
REL="PREVIOUS"
TITLE="Introduction"
HREF="part1.html"><LINK
REL="NEXT"
TITLE="Starting Off With a Sha-Bang"
HREF="sha-bang.html"><META
HTTP-EQUIV="Content-Style-Type"
CONTENT="text/css"><LINK
REL="stylesheet"
HREF="common/kde-common.css"
TYPE="text/css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=iso-8859-1"><META
HTTP-EQUIV="Content-Language"
CONTENT="en"><LINK
REL="stylesheet"
HREF="common/kde-localised.css"
TYPE="text/css"
TITLE="KDE-English"><LINK
REL="stylesheet"
HREF="common/kde-default.css"
TYPE="text/css"
TITLE="KDE-Default"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#AA0000"
VLINK="#AA0055"
ALINK="#AA0000"
STYLE="font-family: sans-serif;"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="part1.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="sha-bang.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="WHY-SHELL"
></A
>Chapter 1. Shell Programming!</H1
><TABLE
BORDER="0"
WIDTH="100%"
CELLSPACING="0"
CELLPADDING="0"
CLASS="EPIGRAPH"
><TR
><TD
WIDTH="45%"
> </TD
><TD
WIDTH="45%"
ALIGN="LEFT"
VALIGN="TOP"
><I
><P
><I
>No programming language is perfect. There is not even a single
best language; there are only languages well suited or perhaps
poorly suited for particular purposes.</I
></P
><P
><I
>--Herbert Mayer</I
></P
></I
></TD
></TR
></TABLE
><P
>A working knowledge of shell scripting is essential to anyone
wishing to become reasonably proficient at system administration,
even if they do not anticipate ever having to actually write a
script. Consider that as a Linux machine boots up, it executes the
shell scripts in <TT
CLASS="FILENAME"
>/etc/rc.d</TT
>
to restore the system configuration and set up services. A detailed
understanding of these startup scripts is important for analyzing
the behavior of a system, and possibly modifying it.</P
><P
>The craft of scripting is not hard to master,
since scripts can be built in bite-sized sections and there
is only a fairly small set of shell-specific operators and options
<A
NAME="AEN62"
HREF="#FTN.AEN62"
>[1]</A
>
to learn. The syntax is simple -- even austere -- similar to
that of invoking and chaining together utilities at the command
line, and there are only a few <SPAN
CLASS="QUOTE"
>"rules"</SPAN
> governing
their use. Most short scripts work right the first time, and
debugging even the longer ones is straightforward.</P
><P
> <A
NAME="AEN67"
></A
><BLOCKQUOTE
CLASS="BLOCKQUOTE"
><P
CLASS="LITERALLAYOUT"
> In the early days of personal computing, the BASIC language enabled<br>
anyone reasonably computer proficient to write programs on an early<br>
generation of microcomputers. Decades later, the Bash scripting<br>
language enables anyone with a rudimentary knowledge of Linux or<br>
UNIX to do the same on modern machines.<br>
<br>
We now have miniaturized single-board computers with amazing<br>
capabilities, such as the <A
HREF="http://www.raspberrypi.org/"
TARGET="_top"
>Raspberry Pi</A
>.<br>
Bash scripting provides a way to explore the capabilities of these<br>
fascinating devices.<br>
</P
></BLOCKQUOTE
>
</P
><P
>A shell script is a quick-and-dirty method of prototyping
a complex application. Getting even a limited subset of
the functionality to work in a script is often a useful
first stage in project development. In this way, the structure
of the application can be tested and tinkered with, and the
major pitfalls found before proceeding to the final coding
in <I
CLASS="FIRSTTERM"
>C</I
>, <I
CLASS="FIRSTTERM"
>C++</I
>,
<I
CLASS="FIRSTTERM"
>Java</I
>, <A
HREF="wrapper.html#PERLREF"
>Perl</A
>,
or <I
CLASS="FIRSTTERM"
>Python</I
>.</P
><P
>Shell scripting hearkens back to the classic UNIX philosophy
of breaking complex projects into simpler subtasks, of chaining
together components and utilities. Many consider this a better,
or at least more esthetically pleasing approach to problem solving
than using one of the new generation of high-powered all-in-one
languages, such as <I
CLASS="FIRSTTERM"
>Perl</I
>, which attempt to
be all things to all people, but at the cost of forcing you to
alter your thinking processes to fit the tool.</P
><P
>According to <A
HREF="biblio.html#MAYERREF"
>Herbert Mayer</A
>,
<SPAN
CLASS="QUOTE"
>"a useful language needs arrays, pointers,
and a generic mechanism for building data structures."</SPAN
>
By these criteria, shell scripting falls somewhat short of being
<SPAN
CLASS="QUOTE"
>"useful."</SPAN
> Or, perhaps not. . . .</P
><TABLE
CLASS="SIDEBAR"
BORDER="1"
CELLPADDING="5"
><TR
><TD
><DIV
CLASS="SIDEBAR"
><A
NAME="AEN82"
></A
><P
>When not to use shell scripts
<UL
><LI
><P
>Resource-intensive tasks, especially where speed is
a factor (sorting, hashing, recursion
<A
NAME="AEN87"
HREF="#FTN.AEN87"
>[2]</A
>
...)</P
></LI
><LI
><P
>Procedures involving heavy-duty math operations,
especially floating point arithmetic, arbitrary
precision calculations, or complex numbers (use
<I
CLASS="FIRSTTERM"
>C++</I
> or <I
CLASS="FIRSTTERM"
>FORTRAN</I
>
instead)</P
></LI
><LI
><P
>Cross-platform portability required (use
<I
CLASS="FIRSTTERM"
>C</I
> or <I
CLASS="FIRSTTERM"
>Java</I
>
instead)</P
></LI
><LI
><P
>Complex applications, where structured programming is
a necessity (type-checking of variables, function
prototypes, etc.)</P
></LI
><LI
><P
>Mission-critical applications upon which you are betting the
future of the company</P
></LI
><LI
><P
>Situations where <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>security</I
></SPAN
> is
important, where you need to guarantee the integrity of
your system and protect against intrusion, cracking, and
vandalism</P
></LI
><LI
><P
>Project consists of subcomponents with interlocking
dependencies</P
></LI
><LI
><P
>Extensive file operations required
(<I
CLASS="FIRSTTERM"
>Bash</I
> is limited to serial file access,
and that only in a particularly clumsy and inefficient
line-by-line fashion.)</P
></LI
><LI
><P
>Need native support for multi-dimensional arrays</P
></LI
><LI
><P
>Need data structures, such as linked lists or trees</P
></LI
><LI
><P
>Need to generate / manipulate graphics or GUIs</P
></LI
><LI
><P
>Need direct access to system hardware or
external peripherals</P
></LI
><LI
><P
>Need port or <A
HREF="devproc.html#SOCKETREF"
>socket</A
>
I/O</P
></LI
><LI
><P
>Need to use libraries or interface with legacy code</P
></LI
><LI
><P
>Proprietary, closed-source applications (Shell scripts
put the source code right out in the open for all the world
to see.)</P
></LI
></UL
></P
><P
>If any of the above applies, consider a more powerful scripting
language -- perhaps <I
CLASS="FIRSTTERM"
>Perl</I
>,
<I
CLASS="FIRSTTERM"
>Tcl</I
>, <I
CLASS="FIRSTTERM"
>Python</I
>,
<I
CLASS="FIRSTTERM"
>Ruby</I
> -- or possibly a
compiled language such as <I
CLASS="FIRSTTERM"
>C</I
>,
<I
CLASS="FIRSTTERM"
>C++</I
>, or <I
CLASS="FIRSTTERM"
>Java</I
>. Even
then, prototyping the application as a shell script might still
be a useful development step.</P
></DIV
></TD
></TR
></TABLE
><P
><A
NAME="BASHDEF"
></A
></P
><P
>We will be using <SPAN
CLASS="ACRONYM"
>Bash</SPAN
>, an acronym
<A
NAME="AEN139"
HREF="#FTN.AEN139"
>[3]</A
>
for <SPAN
CLASS="QUOTE"
>"Bourne-Again shell"</SPAN
> and a pun on Stephen Bourne's
now classic <I
CLASS="FIRSTTERM"
>Bourne</I
> shell. Bash has become
a <I
CLASS="FOREIGNPHRASE"
>de facto</I
> standard for shell
scripting on most flavors of UNIX. Most of the principles this
book covers apply equally well to scripting with other shells,
such as the <I
CLASS="FIRSTTERM"
>Korn Shell</I
>, from which Bash
derives some of its features,
<A
NAME="AEN147"
HREF="#FTN.AEN147"
>[4]</A
>
and the <I
CLASS="FIRSTTERM"
>C Shell</I
> and its variants. (Note that
<I
CLASS="FIRSTTERM"
>C Shell</I
> programming is not recommended due to
certain inherent problems, as pointed out in an October, 1993 <A
HREF="http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/"
TARGET="_top"
>Usenet
post</A
> by Tom Christiansen.) </P
><P
>What follows is a tutorial on shell scripting. It relies
heavily on examples to illustrate various features of the shell.
The example scripts work -- they've been tested, insofar as
possible -- and some of them are even useful in real life. The
reader can play with the actual working code of the examples
in the source archive (<TT
CLASS="FILENAME"
>scriptname.sh</TT
> or
<TT
CLASS="FILENAME"
>scriptname.bash</TT
>),
<A
NAME="AEN157"
HREF="#FTN.AEN157"
>[5]</A
>
give them <I
CLASS="FIRSTTERM"
>execute</I
> permission
(<TT
CLASS="USERINPUT"
><B
>chmod u+rx scriptname</B
></TT
>),
then run them to see what happens. Should the <A
HREF="http://bash.deta.in/abs-guide-latest.tar.bz2"
TARGET="_top"
>source
archive</A
> not be available, then cut-and-paste from the <A
HREF="http://www.tldp.org/LDP/abs/abs-guide.html.tar.gz"
TARGET="_top"
>HTML</A
> or
<A
HREF="http://bash.deta.in/abs-guide.pdf"
TARGET="_top"
>pdf</A
>
rendered versions. Be aware that some of the scripts presented here
introduce features before they are explained, and this may require
the reader to temporarily skip ahead for enlightenment.</P
><P
>Unless otherwise noted, <A
HREF="mailto:thegrendel.abs@gmail.com"
TARGET="_top"
>the author</A
> of this
book wrote the example scripts that follow.</P
><TABLE
BORDER="0"
WIDTH="100%"
CELLSPACING="0"
CELLPADDING="0"
CLASS="EPIGRAPH"
><TR
><TD
WIDTH="45%"
> </TD
><TD
WIDTH="45%"
ALIGN="LEFT"
VALIGN="TOP"
><I
><P
><I
>His countenance was bold and bashed not.</I
></P
><P
><I
>--Edmund Spenser</I
></P
></I
></TD
></TR
></TABLE
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN62"
HREF="why-shell.html#AEN62"
>[1]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>These are referred to as <A
HREF="internal.html#BUILTINREF"
>builtins</A
>, features internal to the
shell.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN87"
HREF="why-shell.html#AEN87"
>[2]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>Although <A
HREF="localvar.html#RECURSIONREF0"
>recursion
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>is</I
></SPAN
> possible in a shell script</A
>,
it tends to be slow and its implementation is often
an <A
HREF="recurnolocvar.html#FIBOREF"
>ugly kludge</A
>.
</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN139"
HREF="why-shell.html#AEN139"
>[3]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>An <I
CLASS="FIRSTTERM"
>acronym</I
>
is an <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>ersatz</I
></SPAN
> word formed by pasting
together the initial letters of the words into a tongue-tripping
phrase. This morally corrupt and pernicious practice
deserves appropriately severe punishment. Public
flogging suggests itself.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN147"
HREF="why-shell.html#AEN147"
>[4]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>Many of the features of <I
CLASS="FIRSTTERM"
>ksh88</I
>,
and even a few from the updated <I
CLASS="FIRSTTERM"
>ksh93</I
>
have been merged into Bash.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN157"
HREF="why-shell.html#AEN157"
>[5]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>By convention, user-written shell scripts
that are Bourne shell compliant generally take a name with a
<TT
CLASS="FILENAME"
>.sh</TT
> extension. System scripts, such as
those found in <TT
CLASS="FILENAME"
>/etc/rc.d</TT
>,
do not necessarily conform to this nomenclature.</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="part1.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="sha-bang.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Introduction</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="part1.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Starting Off With a Sha-Bang</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|