Debian Codegraph @

HTML/why-shell.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Shell Programming!</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
9 "><LINK
10 REL="HOME"
11 TITLE="Advanced Bash-Scripting Guide"
12 HREF="index.html"><LINK
13 REL="UP"
14 TITLE="Introduction"
15 HREF="part1.html"><LINK
16 REL="PREVIOUS"
17 TITLE="Introduction"
18 HREF="part1.html"><LINK
19 REL="NEXT"
20 TITLE="Starting Off With a Sha-Bang"
21 HREF="sha-bang.html"><META
22 HTTP-EQUIV="Content-Style-Type"
23 CONTENT="text/css"><LINK
24 REL="stylesheet"
25 HREF="common/kde-common.css"
26 TYPE="text/css"><META
27 HTTP-EQUIV="Content-Type"
28 CONTENT="text/html; charset=iso-8859-1"><META
29 HTTP-EQUIV="Content-Language"
30 CONTENT="en"><LINK
31 REL="stylesheet"
32 HREF="common/kde-localised.css"
33 TYPE="text/css"
34 TITLE="KDE-English"><LINK
35 REL="stylesheet"
36 HREF="common/kde-default.css"
37 TYPE="text/css"
38 TITLE="KDE-Default"></HEAD
39 ><BODY
40 CLASS="CHAPTER"
41 BGCOLOR="#FFFFFF"
42 TEXT="#000000"
43 LINK="#AA0000"
44 VLINK="#AA0055"
45 ALINK="#AA0000"
46 STYLE="font-family: sans-serif;"
47 ><DIV
48 CLASS="NAVHEADER"
49 ><TABLE
50 SUMMARY="Header navigation table"
51 WIDTH="100%"
52 BORDER="0"
53 CELLPADDING="0"
54 CELLSPACING="0"
55 ><TR
56 ><TH
57 COLSPAN="3"
58 ALIGN="center"
59 >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
60 ></TR
61 ><TR
62 ><TD
63 WIDTH="10%"
64 ALIGN="left"
65 VALIGN="bottom"
66 ><A
67 HREF="part1.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 ></TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="sha-bang.html"
82 ACCESSKEY="N"
83 >Next</A
84 ></TD
85 ></TR
86 ></TABLE
87 ><HR
88 ALIGN="LEFT"
89 WIDTH="100%"></DIV
90 ><DIV
91 CLASS="CHAPTER"
92 ><H1
93 ><A
94 NAME="WHY-SHELL"
95 ></A
96 >Chapter 1. Shell Programming!</H1
97 ><TABLE
98 BORDER="0"
99 WIDTH="100%"
100 CELLSPACING="0"
101 CELLPADDING="0"
102 CLASS="EPIGRAPH"
103 ><TR
104 ><TD
105 WIDTH="45%"
106 >&nbsp;</TD
107 ><TD
108 WIDTH="45%"
109 ALIGN="LEFT"
110 VALIGN="TOP"
111 ><I
112 ><P
113 ><I
114 >No programming language is perfect. There is not even a single
115 best language; there are only languages well suited or perhaps
116 poorly suited for particular purposes.</I
117 ></P
118 ><P
119 ><I
120 >--Herbert Mayer</I
121 ></P
122 ></I
123 ></TD
124 ></TR
125 ></TABLE
126 ><P
127 >A working knowledge of shell scripting is essential to anyone
128 wishing to become reasonably proficient at system administration,
129 even if they do not anticipate ever having to actually write a
130 script. Consider that as a Linux machine boots up, it executes the
131 shell scripts in <TT
132 CLASS="FILENAME"
133 >/etc/rc.d</TT
134 >
135 to restore the system configuration and set up services. A detailed
136 understanding of these startup scripts is important for analyzing
137 the behavior of a system, and possibly modifying it.</P
138 ><P
139 >The craft of scripting is not hard to master,
140 since scripts can be built in bite-sized sections and there
141 is only a fairly small set of shell-specific operators and options
142
143 <A
144 NAME="AEN62"
145 HREF="#FTN.AEN62"
146 >[1]</A
147 >
148
149 to learn. The syntax is simple -- even austere -- similar to
150 that of invoking and chaining together utilities at the command
151 line, and there are only a few <SPAN
152 CLASS="QUOTE"
153 >"rules"</SPAN
154 > governing
155 their use. Most short scripts work right the first time, and
156 debugging even the longer ones is straightforward.</P
157 ><P
158 > <A
159 NAME="AEN67"
160 ></A
161 ><BLOCKQUOTE
162 CLASS="BLOCKQUOTE"
163 ><P
164 CLASS="LITERALLAYOUT"
165 >&nbsp;&nbsp;&nbsp;&nbsp;In&nbsp;the&nbsp;early&nbsp;days&nbsp;of&nbsp;personal&nbsp;computing,&nbsp;the&nbsp;BASIC&nbsp;language&nbsp;enabled<br>
166 &nbsp;&nbsp;&nbsp;&nbsp;anyone&nbsp;reasonably&nbsp;computer&nbsp;proficient&nbsp;to&nbsp;write&nbsp;programs&nbsp;on&nbsp;an&nbsp;early<br>
167 &nbsp;&nbsp;&nbsp;&nbsp;generation&nbsp;of&nbsp;microcomputers.&nbsp;Decades&nbsp;later,&nbsp;the&nbsp;Bash&nbsp;scripting<br>
168 &nbsp;&nbsp;&nbsp;&nbsp;language&nbsp;enables&nbsp;anyone&nbsp;with&nbsp;a&nbsp;rudimentary&nbsp;knowledge&nbsp;of&nbsp;Linux&nbsp;or<br>
169 &nbsp;&nbsp;&nbsp;&nbsp;UNIX&nbsp;to&nbsp;do&nbsp;the&nbsp;same&nbsp;on&nbsp;modern&nbsp;machines.<br>
170 <br>
171 &nbsp;&nbsp;&nbsp;&nbsp;We&nbsp;now&nbsp;have&nbsp;miniaturized&nbsp;single-board&nbsp;computers&nbsp;with&nbsp;amazing<br>
172 &nbsp;&nbsp;&nbsp;&nbsp;capabilities,&nbsp;such&nbsp;as&nbsp;the&nbsp;<A
173 HREF="http://www.raspberrypi.org/"
174 TARGET="_top"
175 >Raspberry Pi</A
176 >.<br>
177 &nbsp;&nbsp;&nbsp;&nbsp;Bash&nbsp;scripting&nbsp;provides&nbsp;a&nbsp;way&nbsp;to&nbsp;explore&nbsp;the&nbsp;capabilities&nbsp;of&nbsp;these<br>
178 &nbsp;&nbsp;&nbsp;&nbsp;fascinating&nbsp;devices.<br>
179 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</P
180 ></BLOCKQUOTE
181 >
182 </P
183 ><P
184 >A shell script is a quick-and-dirty method of prototyping
185 a complex application. Getting even a limited subset of
186 the functionality to work in a script is often a useful
187 first stage in project development. In this way, the structure
188 of the application can be tested and tinkered with, and the
189 major pitfalls found before proceeding to the final coding
190 in <I
191 CLASS="FIRSTTERM"
192 >C</I
193 >, <I
194 CLASS="FIRSTTERM"
195 >C++</I
196 >,
197 <I
198 CLASS="FIRSTTERM"
199 >Java</I
200 >, <A
201 HREF="wrapper.html#PERLREF"
202 >Perl</A
203 >,
204 or <I
205 CLASS="FIRSTTERM"
206 >Python</I
207 >.</P
208 ><P
209 >Shell scripting hearkens back to the classic UNIX philosophy
210 of breaking complex projects into simpler subtasks, of chaining
211 together components and utilities. Many consider this a better,
212 or at least more esthetically pleasing approach to problem solving
213 than using one of the new generation of high-powered all-in-one
214 languages, such as <I
215 CLASS="FIRSTTERM"
216 >Perl</I
217 >, which attempt to
218 be all things to all people, but at the cost of forcing you to
219 alter your thinking processes to fit the tool.</P
220 ><P
221 >According to <A
222 HREF="biblio.html#MAYERREF"
223 >Herbert Mayer</A
224 >,
225 <SPAN
226 CLASS="QUOTE"
227 >"a useful language needs arrays, pointers,
228 and a generic mechanism for building data structures."</SPAN
229 >
230 By these criteria, shell scripting falls somewhat short of being
231 <SPAN
232 CLASS="QUOTE"
233 >"useful."</SPAN
234 > Or, perhaps not. . . .</P
235 ><TABLE
236 CLASS="SIDEBAR"
237 BORDER="1"
238 CELLPADDING="5"
239 ><TR
240 ><TD
241 ><DIV
242 CLASS="SIDEBAR"
243 ><A
244 NAME="AEN82"
245 ></A
246 ><P
247 >When not to use shell scripts
248
249 <UL
250 ><LI
251 ><P
252 >Resource-intensive tasks, especially where speed is
253 a factor (sorting, hashing, recursion
254
255 <A
256 NAME="AEN87"
257 HREF="#FTN.AEN87"
258 >[2]</A
259 >
260
261 ...)</P
262 ></LI
263 ><LI
264 ><P
265 >Procedures involving heavy-duty math operations,
266 especially floating point arithmetic, arbitrary
267 precision calculations, or complex numbers (use
268 <I
269 CLASS="FIRSTTERM"
270 >C++</I
271 > or <I
272 CLASS="FIRSTTERM"
273 >FORTRAN</I
274 >
275 instead)</P
276 ></LI
277 ><LI
278 ><P
279 >Cross-platform portability required (use
280 <I
281 CLASS="FIRSTTERM"
282 >C</I
283 > or <I
284 CLASS="FIRSTTERM"
285 >Java</I
286 >
287 instead)</P
288 ></LI
289 ><LI
290 ><P
291 >Complex applications, where structured programming is
292 a necessity (type-checking of variables, function
293 prototypes, etc.)</P
294 ></LI
295 ><LI
296 ><P
297 >Mission-critical applications upon which you are betting the
298 future of the company</P
299 ></LI
300 ><LI
301 ><P
302 >Situations where <SPAN
303 CLASS="emphasis"
304 ><I
305 CLASS="EMPHASIS"
306 >security</I
307 ></SPAN
308 > is
309 important, where you need to guarantee the integrity of
310 your system and protect against intrusion, cracking, and
311 vandalism</P
312 ></LI
313 ><LI
314 ><P
315 >Project consists of subcomponents with interlocking
316 dependencies</P
317 ></LI
318 ><LI
319 ><P
320 >Extensive file operations required
321 (<I
322 CLASS="FIRSTTERM"
323 >Bash</I
324 > is limited to serial file access,
325 and that only in a particularly clumsy and inefficient
326 line-by-line fashion.)</P
327 ></LI
328 ><LI
329 ><P
330 >Need native support for multi-dimensional arrays</P
331 ></LI
332 ><LI
333 ><P
334 >Need data structures, such as linked lists or trees</P
335 ></LI
336 ><LI
337 ><P
338 >Need to generate / manipulate graphics or GUIs</P
339 ></LI
340 ><LI
341 ><P
342 >Need direct access to system hardware or
343 external peripherals</P
344 ></LI
345 ><LI
346 ><P
347 >Need port or <A
348 HREF="devproc.html#SOCKETREF"
349 >socket</A
350 >
351 I/O</P
352 ></LI
353 ><LI
354 ><P
355 >Need to use libraries or interface with legacy code</P
356 ></LI
357 ><LI
358 ><P
359 >Proprietary, closed-source applications (Shell scripts
360 put the source code right out in the open for all the world
361 to see.)</P
362 ></LI
363 ></UL
364 ></P
365 ><P
366 >If any of the above applies, consider a more powerful scripting
367 language -- perhaps <I
368 CLASS="FIRSTTERM"
369 >Perl</I
370 >,
371 <I
372 CLASS="FIRSTTERM"
373 >Tcl</I
374 >, <I
375 CLASS="FIRSTTERM"
376 >Python</I
377 >,
378 <I
379 CLASS="FIRSTTERM"
380 >Ruby</I
381 > -- or possibly a
382 compiled language such as <I
383 CLASS="FIRSTTERM"
384 >C</I
385 >,
386 <I
387 CLASS="FIRSTTERM"
388 >C++</I
389 >, or <I
390 CLASS="FIRSTTERM"
391 >Java</I
392 >. Even
393 then, prototyping the application as a shell script might still
394 be a useful development step.</P
395 ></DIV
396 ></TD
397 ></TR
398 ></TABLE
399 ><P
400 ><A
401 NAME="BASHDEF"
402 ></A
403 ></P
404 ><P
405 >We will be using <SPAN
406 CLASS="ACRONYM"
407 >Bash</SPAN
408 >, an acronym
409
410 <A
411 NAME="AEN139"
412 HREF="#FTN.AEN139"
413 >[3]</A
414 >
415
416 for <SPAN
417 CLASS="QUOTE"
418 >"Bourne-Again shell"</SPAN
419 > and a pun on Stephen Bourne's
420 now classic <I
421 CLASS="FIRSTTERM"
422 >Bourne</I
423 > shell. Bash has become
424 a <I
425 CLASS="FOREIGNPHRASE"
426 >de facto</I
427 > standard for shell
428 scripting on most flavors of UNIX. Most of the principles this
429 book covers apply equally well to scripting with other shells,
430 such as the <I
431 CLASS="FIRSTTERM"
432 >Korn Shell</I
433 >, from which Bash
434 derives some of its features,
435
436 <A
437 NAME="AEN147"
438 HREF="#FTN.AEN147"
439 >[4]</A
440 >
441
442 and the <I
443 CLASS="FIRSTTERM"
444 >C Shell</I
445 > and its variants. (Note that
446 <I
447 CLASS="FIRSTTERM"
448 >C Shell</I
449 > programming is not recommended due to
450 certain inherent problems, as pointed out in an October, 1993 <A
451 HREF="http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/"
452 TARGET="_top"
453 >Usenet
454 post</A
455 > by Tom Christiansen.) </P
456 ><P
457 >What follows is a tutorial on shell scripting. It relies
458 heavily on examples to illustrate various features of the shell.
459 The example scripts work -- they've been tested, insofar as
460 possible -- and some of them are even useful in real life. The
461 reader can play with the actual working code of the examples
462 in the source archive (<TT
463 CLASS="FILENAME"
464 >scriptname.sh</TT
465 > or
466 <TT
467 CLASS="FILENAME"
468 >scriptname.bash</TT
469 >),
470
471 <A
472 NAME="AEN157"
473 HREF="#FTN.AEN157"
474 >[5]</A
475 >
476
477 give them <I
478 CLASS="FIRSTTERM"
479 >execute</I
480 > permission
481 (<TT
482 CLASS="USERINPUT"
483 ><B
484 >chmod u+rx scriptname</B
485 ></TT
486 >),
487 then run them to see what happens. Should the <A
488 HREF="http://bash.deta.in/abs-guide-latest.tar.bz2"
489 TARGET="_top"
490 >source
491 archive</A
492 > not be available, then cut-and-paste from the <A
493 HREF="http://www.tldp.org/LDP/abs/abs-guide.html.tar.gz"
494 TARGET="_top"
495 >HTML</A
496 > or
497 <A
498 HREF="http://bash.deta.in/abs-guide.pdf"
499 TARGET="_top"
500 >pdf</A
501 >
502 rendered versions. Be aware that some of the scripts presented here
503 introduce features before they are explained, and this may require
504 the reader to temporarily skip ahead for enlightenment.</P
505 ><P
506 >Unless otherwise noted, <A
507 HREF="mailto:[email protected]"
508 TARGET="_top"
509 >the author</A
510 > of this
511 book wrote the example scripts that follow.</P
512 ><TABLE
513 BORDER="0"
514 WIDTH="100%"
515 CELLSPACING="0"
516 CELLPADDING="0"
517 CLASS="EPIGRAPH"
518 ><TR
519 ><TD
520 WIDTH="45%"
521 >&nbsp;</TD
522 ><TD
523 WIDTH="45%"
524 ALIGN="LEFT"
525 VALIGN="TOP"
526 ><I
527 ><P
528 ><I
529 >His countenance was bold and bashed not.</I
530 ></P
531 ><P
532 ><I
533 >--Edmund Spenser</I
534 ></P
535 ></I
536 ></TD
537 ></TR
538 ></TABLE
539 ></DIV
540 ><H3
541 CLASS="FOOTNOTES"
542 >Notes</H3
543 ><TABLE
544 BORDER="0"
545 CLASS="FOOTNOTES"
546 WIDTH="100%"
547 ><TR
548 ><TD
549 ALIGN="LEFT"
550 VALIGN="TOP"
551 WIDTH="5%"
552 ><A
553 NAME="FTN.AEN62"
554 HREF="why-shell.html#AEN62"
555 >[1]</A
556 ></TD
557 ><TD
558 ALIGN="LEFT"
559 VALIGN="TOP"
560 WIDTH="95%"
561 ><P
562 >These are referred to as <A
563 HREF="internal.html#BUILTINREF"
564 >builtins</A
565 >, features internal to the
566 shell.</P
567 ></TD
568 ></TR
569 ><TR
570 ><TD
571 ALIGN="LEFT"
572 VALIGN="TOP"
573 WIDTH="5%"
574 ><A
575 NAME="FTN.AEN87"
576 HREF="why-shell.html#AEN87"
577 >[2]</A
578 ></TD
579 ><TD
580 ALIGN="LEFT"
581 VALIGN="TOP"
582 WIDTH="95%"
583 ><P
584 >Although <A
585 HREF="localvar.html#RECURSIONREF0"
586 >recursion
587 <SPAN
588 CLASS="emphasis"
589 ><I
590 CLASS="EMPHASIS"
591 >is</I
592 ></SPAN
593 > possible in a shell script</A
594 >,
595 it tends to be slow and its implementation is often
596 an <A
597 HREF="recurnolocvar.html#FIBOREF"
598 >ugly kludge</A
599 >.
600 </P
601 ></TD
602 ></TR
603 ><TR
604 ><TD
605 ALIGN="LEFT"
606 VALIGN="TOP"
607 WIDTH="5%"
608 ><A
609 NAME="FTN.AEN139"
610 HREF="why-shell.html#AEN139"
611 >[3]</A
612 ></TD
613 ><TD
614 ALIGN="LEFT"
615 VALIGN="TOP"
616 WIDTH="95%"
617 ><P
618 >An <I
619 CLASS="FIRSTTERM"
620 >acronym</I
621 >
622 is an <SPAN
623 CLASS="emphasis"
624 ><I
625 CLASS="EMPHASIS"
626 >ersatz</I
627 ></SPAN
628 > word formed by pasting
629 together the initial letters of the words into a tongue-tripping
630 phrase. This morally corrupt and pernicious practice
631 deserves appropriately severe punishment. Public
632 flogging suggests itself.</P
633 ></TD
634 ></TR
635 ><TR
636 ><TD
637 ALIGN="LEFT"
638 VALIGN="TOP"
639 WIDTH="5%"
640 ><A
641 NAME="FTN.AEN147"
642 HREF="why-shell.html#AEN147"
643 >[4]</A
644 ></TD
645 ><TD
646 ALIGN="LEFT"
647 VALIGN="TOP"
648 WIDTH="95%"
649 ><P
650 >Many of the features of <I
651 CLASS="FIRSTTERM"
652 >ksh88</I
653 >,
654 and even a few from the updated <I
655 CLASS="FIRSTTERM"
656 >ksh93</I
657 >
658 have been merged into Bash.</P
659 ></TD
660 ></TR
661 ><TR
662 ><TD
663 ALIGN="LEFT"
664 VALIGN="TOP"
665 WIDTH="5%"
666 ><A
667 NAME="FTN.AEN157"
668 HREF="why-shell.html#AEN157"
669 >[5]</A
670 ></TD
671 ><TD
672 ALIGN="LEFT"
673 VALIGN="TOP"
674 WIDTH="95%"
675 ><P
676 >By convention, user-written shell scripts
677 that are Bourne shell compliant generally take a name with a
678 <TT
679 CLASS="FILENAME"
680 >.sh</TT
681 > extension. System scripts, such as
682 those found in <TT
683 CLASS="FILENAME"
684 >/etc/rc.d</TT
685 >,
686 do not necessarily conform to this nomenclature.</P
687 ></TD
688 ></TR
689 ></TABLE
690 ><DIV
691 CLASS="NAVFOOTER"
692 ><HR
693 ALIGN="LEFT"
694 WIDTH="100%"><TABLE
695 SUMMARY="Footer navigation table"
696 WIDTH="100%"
697 BORDER="0"
698 CELLPADDING="0"
699 CELLSPACING="0"
700 ><TR
701 ><TD
702 WIDTH="33%"
703 ALIGN="left"
704 VALIGN="top"
705 ><A
706 HREF="part1.html"
707 ACCESSKEY="P"
708 >Prev</A
709 ></TD
710 ><TD
711 WIDTH="34%"
712 ALIGN="center"
713 VALIGN="top"
714 ><A
715 HREF="index.html"
716 ACCESSKEY="H"
717 >Home</A
718 ></TD
719 ><TD
720 WIDTH="33%"
721 ALIGN="right"
722 VALIGN="top"
723 ><A
724 HREF="sha-bang.html"
725 ACCESSKEY="N"
726 >Next</A
727 ></TD
728 ></TR
729 ><TR
730 ><TD
731 WIDTH="33%"
732 ALIGN="left"
733 VALIGN="top"
734 >Introduction</TD
735 ><TD
736 WIDTH="34%"
737 ALIGN="center"
738 VALIGN="top"
739 ><A
740 HREF="part1.html"
741 ACCESSKEY="U"
742 >Up</A
743 ></TD
744 ><TD
745 WIDTH="33%"
746 ALIGN="right"
747 VALIGN="top"
748 >Starting Off With a Sha-Bang</TD
749 ></TR
750 ></TABLE
751 ></DIV
752 ></BODY
753 ></HTML
754 >