File: bash2.html

package info (click to toggle)
abs-guide 10-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,960 kB
  • sloc: sh: 14,129; makefile: 81
file content (567 lines) | stat: -rw-r--r-- 12,506 bytes parent folder | download | duplicates (4)
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Bash, versions 2, 3, and 4</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="Advanced Topics"
HREF="part5.html"><LINK
REL="PREVIOUS"
TITLE="Shell Scripting Under Windows"
HREF="winscript.html"><LINK
REL="NEXT"
TITLE="Bash, version 3"
HREF="bashver3.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="winscript.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="bashver3.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="BASH2"
></A
>Chapter 37. Bash, versions 2, 3, and 4</H1
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="BASHVER2"
></A
>37.1. Bash, version 2</H1
><P
><A
NAME="BASH2REF"
></A
></P
><P
>        The current version of <I
CLASS="FIRSTTERM"
>Bash</I
>, the one
	you have running on your machine, is most likely version 2.xx.yy,
	3.xx.yy, or 4.xx.yy.
	      <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>echo $BASH_VERSION</B
></TT
>
 <TT
CLASS="COMPUTEROUTPUT"
>3.2.25(1)-release</TT
>
 	      </PRE
></TD
></TR
></TABLE
>
      </P
><P
>The  version 2 update of the classic Bash scripting language
	added array variables, string and parameter expansion, and
	a better method of indirect variable references, among other
	features.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX77"
></A
><P
><B
>Example 37-1. String expansion</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;
   3&nbsp;# String expansion.
   4&nbsp;# Introduced with version 2 of Bash.
   5&nbsp;
   6&nbsp;#  Strings of the form $'xxx'
   7&nbsp;#+ have the standard escaped characters interpreted. 
   8&nbsp;
   9&nbsp;echo $'Ringing bell 3 times \a \a \a'
  10&nbsp;     # May only ring once with certain terminals.
  11&nbsp;     # Or ...
  12&nbsp;     # May not ring at all, depending on terminal settings.
  13&nbsp;echo $'Three form feeds \f \f \f'
  14&nbsp;echo $'10 newlines \n\n\n\n\n\n\n\n\n\n'
  15&nbsp;echo $'\102\141\163\150'
  16&nbsp;     #   B   a   s   h
  17&nbsp;     # Octal equivalent of characters.
  18&nbsp;
  19&nbsp;exit</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
><A
NAME="VARREFNEW"
></A
></P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX78"
></A
><P
><B
>Example 37-2. Indirect variable references - the new way</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;
   3&nbsp;# Indirect variable referencing.
   4&nbsp;# This has a few of the attributes of references in C++.
   5&nbsp;
   6&nbsp;
   7&nbsp;a=letter_of_alphabet
   8&nbsp;letter_of_alphabet=z
   9&nbsp;
  10&nbsp;echo "a = $a"           # Direct reference.
  11&nbsp;
  12&nbsp;echo "Now a = ${!a}"    # Indirect reference.
  13&nbsp;#  The ${!variable} notation is more intuitive than the old
  14&nbsp;#+ eval var1=\$$var2
  15&nbsp;
  16&nbsp;echo
  17&nbsp;
  18&nbsp;t=table_cell_3
  19&nbsp;table_cell_3=24
  20&nbsp;echo "t = ${!t}"                      # t = 24
  21&nbsp;table_cell_3=387
  22&nbsp;echo "Value of t changed to ${!t}"    # 387
  23&nbsp;# No 'eval' necessary.
  24&nbsp;
  25&nbsp;#  This is useful for referencing members of an array or table,
  26&nbsp;#+ or for simulating a multi-dimensional array.
  27&nbsp;#  An indexing option (analogous to pointer arithmetic)
  28&nbsp;#+ would have been nice. Sigh.
  29&nbsp;
  30&nbsp;exit 0
  31&nbsp;
  32&nbsp;# See also, ind-ref.sh example.</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="RESISTOR"
></A
><P
><B
>Example 37-3. Simple database application, using indirect variable
	  referencing</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;# resistor-inventory.sh
   3&nbsp;# Simple database / table-lookup application.
   4&nbsp;
   5&nbsp;# ============================================================== #
   6&nbsp;# Data
   7&nbsp;
   8&nbsp;B1723_value=470                                   # Ohms
   9&nbsp;B1723_powerdissip=.25                             # Watts
  10&nbsp;B1723_colorcode="yellow-violet-brown"             # Color bands
  11&nbsp;B1723_loc=173                                     # Where they are
  12&nbsp;B1723_inventory=78                                # How many
  13&nbsp;
  14&nbsp;B1724_value=1000
  15&nbsp;B1724_powerdissip=.25
  16&nbsp;B1724_colorcode="brown-black-red"
  17&nbsp;B1724_loc=24N
  18&nbsp;B1724_inventory=243
  19&nbsp;
  20&nbsp;B1725_value=10000
  21&nbsp;B1725_powerdissip=.125
  22&nbsp;B1725_colorcode="brown-black-orange"
  23&nbsp;B1725_loc=24N
  24&nbsp;B1725_inventory=89
  25&nbsp;
  26&nbsp;# ============================================================== #
  27&nbsp;
  28&nbsp;
  29&nbsp;echo
  30&nbsp;
  31&nbsp;PS3='Enter catalog number: '
  32&nbsp;
  33&nbsp;echo
  34&nbsp;
  35&nbsp;select catalog_number in "B1723" "B1724" "B1725"
  36&nbsp;do
  37&nbsp;  Inv=${catalog_number}_inventory
  38&nbsp;  Val=${catalog_number}_value
  39&nbsp;  Pdissip=${catalog_number}_powerdissip
  40&nbsp;  Loc=${catalog_number}_loc
  41&nbsp;  Ccode=${catalog_number}_colorcode
  42&nbsp;
  43&nbsp;  echo
  44&nbsp;  echo "Catalog number $catalog_number:"
  45&nbsp;  # Now, retrieve value, using indirect referencing.
  46&nbsp;  echo "There are ${!Inv} of  [${!Val} ohm / ${!Pdissip} watt]\
  47&nbsp;  resistors in stock."  #        ^             ^
  48&nbsp;  # As of Bash 4.2, you can replace "ohm" with \u2126 (using echo -e).
  49&nbsp;  echo "These are located in bin # ${!Loc}."
  50&nbsp;  echo "Their color code is \"${!Ccode}\"."
  51&nbsp;
  52&nbsp;  break
  53&nbsp;done
  54&nbsp;
  55&nbsp;echo; echo
  56&nbsp;
  57&nbsp;# Exercises:
  58&nbsp;# ---------
  59&nbsp;# 1) Rewrite this script to read its data from an external file.
  60&nbsp;# 2) Rewrite this script to use arrays,
  61&nbsp;#+   rather than indirect variable referencing.
  62&nbsp;#    Which method is more straightforward and intuitive?
  63&nbsp;#    Which method is easier to code?
  64&nbsp;
  65&nbsp;
  66&nbsp;# Notes:
  67&nbsp;# -----
  68&nbsp;#  Shell scripts are inappropriate for anything except the most simple
  69&nbsp;#+ database applications, and even then it involves workarounds and kludges.
  70&nbsp;#  Much better is to use a language with native support for data structures,
  71&nbsp;#+ such as C++ or Java (or even Perl).
  72&nbsp;
  73&nbsp;exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="CARDS"
></A
><P
><B
>Example 37-4. Using arrays and other miscellaneous trickery
	  to deal four random hands from a deck of cards</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;# cards.sh
   3&nbsp;
   4&nbsp;# Deals four random hands from a deck of cards.
   5&nbsp;
   6&nbsp;UNPICKED=0
   7&nbsp;PICKED=1
   8&nbsp;
   9&nbsp;DUPE_CARD=99
  10&nbsp;
  11&nbsp;LOWER_LIMIT=0
  12&nbsp;UPPER_LIMIT=51
  13&nbsp;CARDS_IN_SUIT=13
  14&nbsp;CARDS=52
  15&nbsp;
  16&nbsp;declare -a Deck
  17&nbsp;declare -a Suits
  18&nbsp;declare -a Cards
  19&nbsp;#  It would have been easier to implement and more intuitive
  20&nbsp;#+ with a single, 3-dimensional array.
  21&nbsp;#  Perhaps a future version of Bash will support multidimensional arrays.
  22&nbsp;
  23&nbsp;
  24&nbsp;initialize_Deck ()
  25&nbsp;{
  26&nbsp;i=$LOWER_LIMIT
  27&nbsp;until [ "$i" -gt $UPPER_LIMIT ]
  28&nbsp;do
  29&nbsp;  Deck[i]=$UNPICKED   # Set each card of "Deck" as unpicked.
  30&nbsp;  let "i += 1"
  31&nbsp;done
  32&nbsp;echo
  33&nbsp;}
  34&nbsp;
  35&nbsp;initialize_Suits ()
  36&nbsp;{
  37&nbsp;Suits[0]=C #Clubs
  38&nbsp;Suits[1]=D #Diamonds
  39&nbsp;Suits[2]=H #Hearts
  40&nbsp;Suits[3]=S #Spades
  41&nbsp;}
  42&nbsp;
  43&nbsp;initialize_Cards ()
  44&nbsp;{
  45&nbsp;Cards=(2 3 4 5 6 7 8 9 10 J Q K A)
  46&nbsp;# Alternate method of initializing an array.
  47&nbsp;}
  48&nbsp;
  49&nbsp;pick_a_card ()
  50&nbsp;{
  51&nbsp;card_number=$RANDOM
  52&nbsp;let "card_number %= $CARDS" # Restrict range to 0 - 51, i.e., 52 cards.
  53&nbsp;if [ "${Deck[card_number]}" -eq $UNPICKED ]
  54&nbsp;then
  55&nbsp;  Deck[card_number]=$PICKED
  56&nbsp;  return $card_number
  57&nbsp;else  
  58&nbsp;  return $DUPE_CARD
  59&nbsp;fi
  60&nbsp;}
  61&nbsp;
  62&nbsp;parse_card ()
  63&nbsp;{
  64&nbsp;number=$1
  65&nbsp;let "suit_number = number / CARDS_IN_SUIT"
  66&nbsp;suit=${Suits[suit_number]}
  67&nbsp;echo -n "$suit-"
  68&nbsp;let "card_no = number % CARDS_IN_SUIT"
  69&nbsp;Card=${Cards[card_no]}
  70&nbsp;printf %-4s $Card
  71&nbsp;# Print cards in neat columns.
  72&nbsp;}
  73&nbsp;
  74&nbsp;seed_random ()  # Seed random number generator.
  75&nbsp;{               # What happens if you don't do this?
  76&nbsp;seed=`eval date +%s`
  77&nbsp;let "seed %= 32766"
  78&nbsp;RANDOM=$seed
  79&nbsp;} # Consider other methods of seeding the random number generator.
  80&nbsp;
  81&nbsp;deal_cards ()
  82&nbsp;{
  83&nbsp;echo
  84&nbsp;
  85&nbsp;cards_picked=0
  86&nbsp;while [ "$cards_picked" -le $UPPER_LIMIT ]
  87&nbsp;do
  88&nbsp;  pick_a_card
  89&nbsp;  t=$?
  90&nbsp;
  91&nbsp;  if [ "$t" -ne $DUPE_CARD ]
  92&nbsp;  then
  93&nbsp;    parse_card $t
  94&nbsp;
  95&nbsp;    u=$cards_picked+1
  96&nbsp;    # Change back to 1-based indexing, temporarily. Why?
  97&nbsp;    let "u %= $CARDS_IN_SUIT"
  98&nbsp;    if [ "$u" -eq 0 ]   # Nested if/then condition test.
  99&nbsp;    then
 100&nbsp;     echo
 101&nbsp;     echo
 102&nbsp;    fi                  # Each hand set apart with a blank line.
 103&nbsp;
 104&nbsp;    let "cards_picked += 1"
 105&nbsp;  fi  
 106&nbsp;done  
 107&nbsp;
 108&nbsp;echo
 109&nbsp;
 110&nbsp;return 0
 111&nbsp;}
 112&nbsp;
 113&nbsp;
 114&nbsp;# Structured programming:
 115&nbsp;# Entire program logic modularized in functions.
 116&nbsp;
 117&nbsp;#===============
 118&nbsp;seed_random
 119&nbsp;initialize_Deck
 120&nbsp;initialize_Suits
 121&nbsp;initialize_Cards
 122&nbsp;deal_cards
 123&nbsp;#===============
 124&nbsp;
 125&nbsp;exit
 126&nbsp;
 127&nbsp;
 128&nbsp;
 129&nbsp;# Exercise 1:
 130&nbsp;# Add comments to thoroughly document this script.
 131&nbsp;
 132&nbsp;# Exercise 2:
 133&nbsp;# Add a routine (function) to print out each hand sorted in suits.
 134&nbsp;# You may add other bells and whistles if you like.
 135&nbsp;
 136&nbsp;# Exercise 3:
 137&nbsp;# Simplify and streamline the logic of the script.</PRE
></TD
></TR
></TABLE
><HR></DIV
></DIV
></DIV
><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="winscript.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="bashver3.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Shell Scripting Under Windows</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="part5.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Bash, version 3</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>