Debian Codegraph @

HTML/bash2.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Bash, versions 2, 3, and 4</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="Advanced Topics"
15 HREF="part5.html"><LINK
16 REL="PREVIOUS"
17 TITLE="Shell Scripting Under Windows"
18 HREF="winscript.html"><LINK
19 REL="NEXT"
20 TITLE="Bash, version 3"
21 HREF="bashver3.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="winscript.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="bashver3.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="BASH2"
95 ></A
96 >Chapter 37. Bash, versions 2, 3, and 4</H1
97 ><DIV
98 CLASS="SECT1"
99 ><H1
100 CLASS="SECT1"
101 ><A
102 NAME="BASHVER2"
103 ></A
104 >37.1. Bash, version 2</H1
105 ><P
106 ><A
107 NAME="BASH2REF"
108 ></A
109 ></P
110 ><P
111 > The current version of <I
112 CLASS="FIRSTTERM"
113 >Bash</I
114 >, the one
115 you have running on your machine, is most likely version 2.xx.yy,
116 3.xx.yy, or 4.xx.yy.
117 <TABLE
118 BORDER="0"
119 BGCOLOR="#E0E0E0"
120 WIDTH="100%"
121 ><TR
122 ><TD
123 ><PRE
124 CLASS="SCREEN"
125 > <TT
126 CLASS="PROMPT"
127 >bash$ </TT
128 ><TT
129 CLASS="USERINPUT"
130 ><B
131 >echo $BASH_VERSION</B
132 ></TT
133 >
134 <TT
135 CLASS="COMPUTEROUTPUT"
136 >3.2.25(1)-release</TT
137 >
138 </PRE
139 ></TD
140 ></TR
141 ></TABLE
142 >
143 </P
144 ><P
145 >The version 2 update of the classic Bash scripting language
146 added array variables, string and parameter expansion, and
147 a better method of indirect variable references, among other
148 features.</P
149 ><DIV
150 CLASS="EXAMPLE"
151 ><HR><A
152 NAME="EX77"
153 ></A
154 ><P
155 ><B
156 >Example 37-1. String expansion</B
157 ></P
158 ><TABLE
159 BORDER="0"
160 BGCOLOR="#E0E0E0"
161 WIDTH="100%"
162 ><TR
163 ><TD
164 ><PRE
165 CLASS="PROGRAMLISTING"
166 > 1&nbsp;#!/bin/bash
167 2&nbsp;
168 3&nbsp;# String expansion.
169 4&nbsp;# Introduced with version 2 of Bash.
170 5&nbsp;
171 6&nbsp;# Strings of the form $'xxx'
172 7&nbsp;#+ have the standard escaped characters interpreted.
173 8&nbsp;
174 9&nbsp;echo $'Ringing bell 3 times \a \a \a'
175 10&nbsp; # May only ring once with certain terminals.
176 11&nbsp; # Or ...
177 12&nbsp; # May not ring at all, depending on terminal settings.
178 13&nbsp;echo $'Three form feeds \f \f \f'
179 14&nbsp;echo $'10 newlines \n\n\n\n\n\n\n\n\n\n'
180 15&nbsp;echo $'\102\141\163\150'
181 16&nbsp; # B a s h
182 17&nbsp; # Octal equivalent of characters.
183 18&nbsp;
184 19&nbsp;exit</PRE
185 ></TD
186 ></TR
187 ></TABLE
188 ><HR></DIV
189 ><P
190 ><A
191 NAME="VARREFNEW"
192 ></A
193 ></P
194 ><DIV
195 CLASS="EXAMPLE"
196 ><HR><A
197 NAME="EX78"
198 ></A
199 ><P
200 ><B
201 >Example 37-2. Indirect variable references - the new way</B
202 ></P
203 ><TABLE
204 BORDER="0"
205 BGCOLOR="#E0E0E0"
206 WIDTH="100%"
207 ><TR
208 ><TD
209 ><PRE
210 CLASS="PROGRAMLISTING"
211 > 1&nbsp;#!/bin/bash
212 2&nbsp;
213 3&nbsp;# Indirect variable referencing.
214 4&nbsp;# This has a few of the attributes of references in C++.
215 5&nbsp;
216 6&nbsp;
217 7&nbsp;a=letter_of_alphabet
218 8&nbsp;letter_of_alphabet=z
219 9&nbsp;
220 10&nbsp;echo "a = $a" # Direct reference.
221 11&nbsp;
222 12&nbsp;echo "Now a = ${!a}" # Indirect reference.
223 13&nbsp;# The ${!variable} notation is more intuitive than the old
224 14&nbsp;#+ eval var1=\$$var2
225 15&nbsp;
226 16&nbsp;echo
227 17&nbsp;
228 18&nbsp;t=table_cell_3
229 19&nbsp;table_cell_3=24
230 20&nbsp;echo "t = ${!t}" # t = 24
231 21&nbsp;table_cell_3=387
232 22&nbsp;echo "Value of t changed to ${!t}" # 387
233 23&nbsp;# No 'eval' necessary.
234 24&nbsp;
235 25&nbsp;# This is useful for referencing members of an array or table,
236 26&nbsp;#+ or for simulating a multi-dimensional array.
237 27&nbsp;# An indexing option (analogous to pointer arithmetic)
238 28&nbsp;#+ would have been nice. Sigh.
239 29&nbsp;
240 30&nbsp;exit 0
241 31&nbsp;
242 32&nbsp;# See also, ind-ref.sh example.</PRE
243 ></TD
244 ></TR
245 ></TABLE
246 ><HR></DIV
247 ><DIV
248 CLASS="EXAMPLE"
249 ><HR><A
250 NAME="RESISTOR"
251 ></A
252 ><P
253 ><B
254 >Example 37-3. Simple database application, using indirect variable
255 referencing</B
256 ></P
257 ><TABLE
258 BORDER="0"
259 BGCOLOR="#E0E0E0"
260 WIDTH="100%"
261 ><TR
262 ><TD
263 ><PRE
264 CLASS="PROGRAMLISTING"
265 > 1&nbsp;#!/bin/bash
266 2&nbsp;# resistor-inventory.sh
267 3&nbsp;# Simple database / table-lookup application.
268 4&nbsp;
269 5&nbsp;# ============================================================== #
270 6&nbsp;# Data
271 7&nbsp;
272 8&nbsp;B1723_value=470 # Ohms
273 9&nbsp;B1723_powerdissip=.25 # Watts
274 10&nbsp;B1723_colorcode="yellow-violet-brown" # Color bands
275 11&nbsp;B1723_loc=173 # Where they are
276 12&nbsp;B1723_inventory=78 # How many
277 13&nbsp;
278 14&nbsp;B1724_value=1000
279 15&nbsp;B1724_powerdissip=.25
280 16&nbsp;B1724_colorcode="brown-black-red"
281 17&nbsp;B1724_loc=24N
282 18&nbsp;B1724_inventory=243
283 19&nbsp;
284 20&nbsp;B1725_value=10000
285 21&nbsp;B1725_powerdissip=.125
286 22&nbsp;B1725_colorcode="brown-black-orange"
287 23&nbsp;B1725_loc=24N
288 24&nbsp;B1725_inventory=89
289 25&nbsp;
290 26&nbsp;# ============================================================== #
291 27&nbsp;
292 28&nbsp;
293 29&nbsp;echo
294 30&nbsp;
295 31&nbsp;PS3='Enter catalog number: '
296 32&nbsp;
297 33&nbsp;echo
298 34&nbsp;
299 35&nbsp;select catalog_number in "B1723" "B1724" "B1725"
300 36&nbsp;do
301 37&nbsp; Inv=${catalog_number}_inventory
302 38&nbsp; Val=${catalog_number}_value
303 39&nbsp; Pdissip=${catalog_number}_powerdissip
304 40&nbsp; Loc=${catalog_number}_loc
305 41&nbsp; Ccode=${catalog_number}_colorcode
306 42&nbsp;
307 43&nbsp; echo
308 44&nbsp; echo "Catalog number $catalog_number:"
309 45&nbsp; # Now, retrieve value, using indirect referencing.
310 46&nbsp; echo "There are ${!Inv} of [${!Val} ohm / ${!Pdissip} watt]\
311 47&nbsp; resistors in stock." # ^ ^
312 48&nbsp; # As of Bash 4.2, you can replace "ohm" with \u2126 (using echo -e).
313 49&nbsp; echo "These are located in bin # ${!Loc}."
314 50&nbsp; echo "Their color code is \"${!Ccode}\"."
315 51&nbsp;
316 52&nbsp; break
317 53&nbsp;done
318 54&nbsp;
319 55&nbsp;echo; echo
320 56&nbsp;
321 57&nbsp;# Exercises:
322 58&nbsp;# ---------
323 59&nbsp;# 1) Rewrite this script to read its data from an external file.
324 60&nbsp;# 2) Rewrite this script to use arrays,
325 61&nbsp;#+ rather than indirect variable referencing.
326 62&nbsp;# Which method is more straightforward and intuitive?
327 63&nbsp;# Which method is easier to code?
328 64&nbsp;
329 65&nbsp;
330 66&nbsp;# Notes:
331 67&nbsp;# -----
332 68&nbsp;# Shell scripts are inappropriate for anything except the most simple
333 69&nbsp;#+ database applications, and even then it involves workarounds and kludges.
334 70&nbsp;# Much better is to use a language with native support for data structures,
335 71&nbsp;#+ such as C++ or Java (or even Perl).
336 72&nbsp;
337 73&nbsp;exit 0</PRE
338 ></TD
339 ></TR
340 ></TABLE
341 ><HR></DIV
342 ><DIV
343 CLASS="EXAMPLE"
344 ><HR><A
345 NAME="CARDS"
346 ></A
347 ><P
348 ><B
349 >Example 37-4. Using arrays and other miscellaneous trickery
350 to deal four random hands from a deck of cards</B
351 ></P
352 ><TABLE
353 BORDER="0"
354 BGCOLOR="#E0E0E0"
355 WIDTH="100%"
356 ><TR
357 ><TD
358 ><PRE
359 CLASS="PROGRAMLISTING"
360 > 1&nbsp;#!/bin/bash
361 2&nbsp;# cards.sh
362 3&nbsp;
363 4&nbsp;# Deals four random hands from a deck of cards.
364 5&nbsp;
365 6&nbsp;UNPICKED=0
366 7&nbsp;PICKED=1
367 8&nbsp;
368 9&nbsp;DUPE_CARD=99
369 10&nbsp;
370 11&nbsp;LOWER_LIMIT=0
371 12&nbsp;UPPER_LIMIT=51
372 13&nbsp;CARDS_IN_SUIT=13
373 14&nbsp;CARDS=52
374 15&nbsp;
375 16&nbsp;declare -a Deck
376 17&nbsp;declare -a Suits
377 18&nbsp;declare -a Cards
378 19&nbsp;# It would have been easier to implement and more intuitive
379 20&nbsp;#+ with a single, 3-dimensional array.
380 21&nbsp;# Perhaps a future version of Bash will support multidimensional arrays.
381 22&nbsp;
382 23&nbsp;
383 24&nbsp;initialize_Deck ()
384 25&nbsp;{
385 26&nbsp;i=$LOWER_LIMIT
386 27&nbsp;until [ "$i" -gt $UPPER_LIMIT ]
387 28&nbsp;do
388 29&nbsp; Deck[i]=$UNPICKED # Set each card of "Deck" as unpicked.
389 30&nbsp; let "i += 1"
390 31&nbsp;done
391 32&nbsp;echo
392 33&nbsp;}
393 34&nbsp;
394 35&nbsp;initialize_Suits ()
395 36&nbsp;{
396 37&nbsp;Suits[0]=C #Clubs
397 38&nbsp;Suits[1]=D #Diamonds
398 39&nbsp;Suits[2]=H #Hearts
399 40&nbsp;Suits[3]=S #Spades
400 41&nbsp;}
401 42&nbsp;
402 43&nbsp;initialize_Cards ()
403 44&nbsp;{
404 45&nbsp;Cards=(2 3 4 5 6 7 8 9 10 J Q K A)
405 46&nbsp;# Alternate method of initializing an array.
406 47&nbsp;}
407 48&nbsp;
408 49&nbsp;pick_a_card ()
409 50&nbsp;{
410 51&nbsp;card_number=$RANDOM
411 52&nbsp;let "card_number %= $CARDS" # Restrict range to 0 - 51, i.e., 52 cards.
412 53&nbsp;if [ "${Deck[card_number]}" -eq $UNPICKED ]
413 54&nbsp;then
414 55&nbsp; Deck[card_number]=$PICKED
415 56&nbsp; return $card_number
416 57&nbsp;else
417 58&nbsp; return $DUPE_CARD
418 59&nbsp;fi
419 60&nbsp;}
420 61&nbsp;
421 62&nbsp;parse_card ()
422 63&nbsp;{
423 64&nbsp;number=$1
424 65&nbsp;let "suit_number = number / CARDS_IN_SUIT"
425 66&nbsp;suit=${Suits[suit_number]}
426 67&nbsp;echo -n "$suit-"
427 68&nbsp;let "card_no = number % CARDS_IN_SUIT"
428 69&nbsp;Card=${Cards[card_no]}
429 70&nbsp;printf %-4s $Card
430 71&nbsp;# Print cards in neat columns.
431 72&nbsp;}
432 73&nbsp;
433 74&nbsp;seed_random () # Seed random number generator.
434 75&nbsp;{ # What happens if you don't do this?
435 76&nbsp;seed=`eval date +%s`
436 77&nbsp;let "seed %= 32766"
437 78&nbsp;RANDOM=$seed
438 79&nbsp;} # Consider other methods of seeding the random number generator.
439 80&nbsp;
440 81&nbsp;deal_cards ()
441 82&nbsp;{
442 83&nbsp;echo
443 84&nbsp;
444 85&nbsp;cards_picked=0
445 86&nbsp;while [ "$cards_picked" -le $UPPER_LIMIT ]
446 87&nbsp;do
447 88&nbsp; pick_a_card
448 89&nbsp; t=$?
449 90&nbsp;
450 91&nbsp; if [ "$t" -ne $DUPE_CARD ]
451 92&nbsp; then
452 93&nbsp; parse_card $t
453 94&nbsp;
454 95&nbsp; u=$cards_picked+1
455 96&nbsp; # Change back to 1-based indexing, temporarily. Why?
456 97&nbsp; let "u %= $CARDS_IN_SUIT"
457 98&nbsp; if [ "$u" -eq 0 ] # Nested if/then condition test.
458 99&nbsp; then
459 100&nbsp; echo
460 101&nbsp; echo
461 102&nbsp; fi # Each hand set apart with a blank line.
462 103&nbsp;
463 104&nbsp; let "cards_picked += 1"
464 105&nbsp; fi
465 106&nbsp;done
466 107&nbsp;
467 108&nbsp;echo
468 109&nbsp;
469 110&nbsp;return 0
470 111&nbsp;}
471 112&nbsp;
472 113&nbsp;
473 114&nbsp;# Structured programming:
474 115&nbsp;# Entire program logic modularized in functions.
475 116&nbsp;
476 117&nbsp;#===============
477 118&nbsp;seed_random
478 119&nbsp;initialize_Deck
479 120&nbsp;initialize_Suits
480 121&nbsp;initialize_Cards
481 122&nbsp;deal_cards
482 123&nbsp;#===============
483 124&nbsp;
484 125&nbsp;exit
485 126&nbsp;
486 127&nbsp;
487 128&nbsp;
488 129&nbsp;# Exercise 1:
489 130&nbsp;# Add comments to thoroughly document this script.
490 131&nbsp;
491 132&nbsp;# Exercise 2:
492 133&nbsp;# Add a routine (function) to print out each hand sorted in suits.
493 134&nbsp;# You may add other bells and whistles if you like.
494 135&nbsp;
495 136&nbsp;# Exercise 3:
496 137&nbsp;# Simplify and streamline the logic of the script.</PRE
497 ></TD
498 ></TR
499 ></TABLE
500 ><HR></DIV
501 ></DIV
502 ></DIV
503 ><DIV
504 CLASS="NAVFOOTER"
505 ><HR
506 ALIGN="LEFT"
507 WIDTH="100%"><TABLE
508 SUMMARY="Footer navigation table"
509 WIDTH="100%"
510 BORDER="0"
511 CELLPADDING="0"
512 CELLSPACING="0"
513 ><TR
514 ><TD
515 WIDTH="33%"
516 ALIGN="left"
517 VALIGN="top"
518 ><A
519 HREF="winscript.html"
520 ACCESSKEY="P"
521 >Prev</A
522 ></TD
523 ><TD
524 WIDTH="34%"
525 ALIGN="center"
526 VALIGN="top"
527 ><A
528 HREF="index.html"
529 ACCESSKEY="H"
530 >Home</A
531 ></TD
532 ><TD
533 WIDTH="33%"
534 ALIGN="right"
535 VALIGN="top"
536 ><A
537 HREF="bashver3.html"
538 ACCESSKEY="N"
539 >Next</A
540 ></TD
541 ></TR
542 ><TR
543 ><TD
544 WIDTH="33%"
545 ALIGN="left"
546 VALIGN="top"
547 >Shell Scripting Under Windows</TD
548 ><TD
549 WIDTH="34%"
550 ALIGN="center"
551 VALIGN="top"
552 ><A
553 HREF="part5.html"
554 ACCESSKEY="U"
555 >Up</A
556 ></TD
557 ><TD
558 WIDTH="33%"
559 ALIGN="right"
560 VALIGN="top"
561 >Bash, version 3</TD
562 ></TR
563 ></TABLE
564 ></DIV
565 ></BODY
566 ></HTML
567 >