| 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 #!/bin/bash |
| 167 | 2 |
| 168 | 3 # String expansion. |
| 169 | 4 # Introduced with version 2 of Bash. |
| 170 | 5 |
| 171 | 6 # Strings of the form $'xxx' |
| 172 | 7 #+ have the standard escaped characters interpreted. |
| 173 | 8 |
| 174 | 9 echo $'Ringing bell 3 times \a \a \a' |
| 175 | 10 # May only ring once with certain terminals. |
| 176 | 11 # Or ... |
| 177 | 12 # May not ring at all, depending on terminal settings. |
| 178 | 13 echo $'Three form feeds \f \f \f' |
| 179 | 14 echo $'10 newlines \n\n\n\n\n\n\n\n\n\n' |
| 180 | 15 echo $'\102\141\163\150' |
| 181 | 16 # B a s h |
| 182 | 17 # Octal equivalent of characters. |
| 183 | 18 |
| 184 | 19 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 #!/bin/bash |
| 212 | 2 |
| 213 | 3 # Indirect variable referencing. |
| 214 | 4 # This has a few of the attributes of references in C++. |
| 215 | 5 |
| 216 | 6 |
| 217 | 7 a=letter_of_alphabet |
| 218 | 8 letter_of_alphabet=z |
| 219 | 9 |
| 220 | 10 echo "a = $a" # Direct reference. |
| 221 | 11 |
| 222 | 12 echo "Now a = ${!a}" # Indirect reference. |
| 223 | 13 # The ${!variable} notation is more intuitive than the old |
| 224 | 14 #+ eval var1=\$$var2 |
| 225 | 15 |
| 226 | 16 echo |
| 227 | 17 |
| 228 | 18 t=table_cell_3 |
| 229 | 19 table_cell_3=24 |
| 230 | 20 echo "t = ${!t}" # t = 24 |
| 231 | 21 table_cell_3=387 |
| 232 | 22 echo "Value of t changed to ${!t}" # 387 |
| 233 | 23 # No 'eval' necessary. |
| 234 | 24 |
| 235 | 25 # This is useful for referencing members of an array or table, |
| 236 | 26 #+ or for simulating a multi-dimensional array. |
| 237 | 27 # An indexing option (analogous to pointer arithmetic) |
| 238 | 28 #+ would have been nice. Sigh. |
| 239 | 29 |
| 240 | 30 exit 0 |
| 241 | 31 |
| 242 | 32 # 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 #!/bin/bash |
| 266 | 2 # resistor-inventory.sh |
| 267 | 3 # Simple database / table-lookup application. |
| 268 | 4 |
| 269 | 5 # ============================================================== # |
| 270 | 6 # Data |
| 271 | 7 |
| 272 | 8 B1723_value=470 # Ohms |
| 273 | 9 B1723_powerdissip=.25 # Watts |
| 274 | 10 B1723_colorcode="yellow-violet-brown" # Color bands |
| 275 | 11 B1723_loc=173 # Where they are |
| 276 | 12 B1723_inventory=78 # How many |
| 277 | 13 |
| 278 | 14 B1724_value=1000 |
| 279 | 15 B1724_powerdissip=.25 |
| 280 | 16 B1724_colorcode="brown-black-red" |
| 281 | 17 B1724_loc=24N |
| 282 | 18 B1724_inventory=243 |
| 283 | 19 |
| 284 | 20 B1725_value=10000 |
| 285 | 21 B1725_powerdissip=.125 |
| 286 | 22 B1725_colorcode="brown-black-orange" |
| 287 | 23 B1725_loc=24N |
| 288 | 24 B1725_inventory=89 |
| 289 | 25 |
| 290 | 26 # ============================================================== # |
| 291 | 27 |
| 292 | 28 |
| 293 | 29 echo |
| 294 | 30 |
| 295 | 31 PS3='Enter catalog number: ' |
| 296 | 32 |
| 297 | 33 echo |
| 298 | 34 |
| 299 | 35 select catalog_number in "B1723" "B1724" "B1725" |
| 300 | 36 do |
| 301 | 37 Inv=${catalog_number}_inventory |
| 302 | 38 Val=${catalog_number}_value |
| 303 | 39 Pdissip=${catalog_number}_powerdissip |
| 304 | 40 Loc=${catalog_number}_loc |
| 305 | 41 Ccode=${catalog_number}_colorcode |
| 306 | 42 |
| 307 | 43 echo |
| 308 | 44 echo "Catalog number $catalog_number:" |
| 309 | 45 # Now, retrieve value, using indirect referencing. |
| 310 | 46 echo "There are ${!Inv} of [${!Val} ohm / ${!Pdissip} watt]\ |
| 311 | 47 resistors in stock." # ^ ^ |
| 312 | 48 # As of Bash 4.2, you can replace "ohm" with \u2126 (using echo -e). |
| 313 | 49 echo "These are located in bin # ${!Loc}." |
| 314 | 50 echo "Their color code is \"${!Ccode}\"." |
| 315 | 51 |
| 316 | 52 break |
| 317 | 53 done |
| 318 | 54 |
| 319 | 55 echo; echo |
| 320 | 56 |
| 321 | 57 # Exercises: |
| 322 | 58 # --------- |
| 323 | 59 # 1) Rewrite this script to read its data from an external file. |
| 324 | 60 # 2) Rewrite this script to use arrays, |
| 325 | 61 #+ rather than indirect variable referencing. |
| 326 | 62 # Which method is more straightforward and intuitive? |
| 327 | 63 # Which method is easier to code? |
| 328 | 64 |
| 329 | 65 |
| 330 | 66 # Notes: |
| 331 | 67 # ----- |
| 332 | 68 # Shell scripts are inappropriate for anything except the most simple |
| 333 | 69 #+ database applications, and even then it involves workarounds and kludges. |
| 334 | 70 # Much better is to use a language with native support for data structures, |
| 335 | 71 #+ such as C++ or Java (or even Perl). |
| 336 | 72 |
| 337 | 73 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 #!/bin/bash |
| 361 | 2 # cards.sh |
| 362 | 3 |
| 363 | 4 # Deals four random hands from a deck of cards. |
| 364 | 5 |
| 365 | 6 UNPICKED=0 |
| 366 | 7 PICKED=1 |
| 367 | 8 |
| 368 | 9 DUPE_CARD=99 |
| 369 | 10 |
| 370 | 11 LOWER_LIMIT=0 |
| 371 | 12 UPPER_LIMIT=51 |
| 372 | 13 CARDS_IN_SUIT=13 |
| 373 | 14 CARDS=52 |
| 374 | 15 |
| 375 | 16 declare -a Deck |
| 376 | 17 declare -a Suits |
| 377 | 18 declare -a Cards |
| 378 | 19 # It would have been easier to implement and more intuitive |
| 379 | 20 #+ with a single, 3-dimensional array. |
| 380 | 21 # Perhaps a future version of Bash will support multidimensional arrays. |
| 381 | 22 |
| 382 | 23 |
| 383 | 24 initialize_Deck () |
| 384 | 25 { |
| 385 | 26 i=$LOWER_LIMIT |
| 386 | 27 until [ "$i" -gt $UPPER_LIMIT ] |
| 387 | 28 do |
| 388 | 29 Deck[i]=$UNPICKED # Set each card of "Deck" as unpicked. |
| 389 | 30 let "i += 1" |
| 390 | 31 done |
| 391 | 32 echo |
| 392 | 33 } |
| 393 | 34 |
| 394 | 35 initialize_Suits () |
| 395 | 36 { |
| 396 | 37 Suits[0]=C #Clubs |
| 397 | 38 Suits[1]=D #Diamonds |
| 398 | 39 Suits[2]=H #Hearts |
| 399 | 40 Suits[3]=S #Spades |
| 400 | 41 } |
| 401 | 42 |
| 402 | 43 initialize_Cards () |
| 403 | 44 { |
| 404 | 45 Cards=(2 3 4 5 6 7 8 9 10 J Q K A) |
| 405 | 46 # Alternate method of initializing an array. |
| 406 | 47 } |
| 407 | 48 |
| 408 | 49 pick_a_card () |
| 409 | 50 { |
| 410 | 51 card_number=$RANDOM |
| 411 | 52 let "card_number %= $CARDS" # Restrict range to 0 - 51, i.e., 52 cards. |
| 412 | 53 if [ "${Deck[card_number]}" -eq $UNPICKED ] |
| 413 | 54 then |
| 414 | 55 Deck[card_number]=$PICKED |
| 415 | 56 return $card_number |
| 416 | 57 else |
| 417 | 58 return $DUPE_CARD |
| 418 | 59 fi |
| 419 | 60 } |
| 420 | 61 |
| 421 | 62 parse_card () |
| 422 | 63 { |
| 423 | 64 number=$1 |
| 424 | 65 let "suit_number = number / CARDS_IN_SUIT" |
| 425 | 66 suit=${Suits[suit_number]} |
| 426 | 67 echo -n "$suit-" |
| 427 | 68 let "card_no = number % CARDS_IN_SUIT" |
| 428 | 69 Card=${Cards[card_no]} |
| 429 | 70 printf %-4s $Card |
| 430 | 71 # Print cards in neat columns. |
| 431 | 72 } |
| 432 | 73 |
| 433 | 74 seed_random () # Seed random number generator. |
| 434 | 75 { # What happens if you don't do this? |
| 435 | 76 seed=`eval date +%s` |
| 436 | 77 let "seed %= 32766" |
| 437 | 78 RANDOM=$seed |
| 438 | 79 } # Consider other methods of seeding the random number generator. |
| 439 | 80 |
| 440 | 81 deal_cards () |
| 441 | 82 { |
| 442 | 83 echo |
| 443 | 84 |
| 444 | 85 cards_picked=0 |
| 445 | 86 while [ "$cards_picked" -le $UPPER_LIMIT ] |
| 446 | 87 do |
| 447 | 88 pick_a_card |
| 448 | 89 t=$? |
| 449 | 90 |
| 450 | 91 if [ "$t" -ne $DUPE_CARD ] |
| 451 | 92 then |
| 452 | 93 parse_card $t |
| 453 | 94 |
| 454 | 95 u=$cards_picked+1 |
| 455 | 96 # Change back to 1-based indexing, temporarily. Why? |
| 456 | 97 let "u %= $CARDS_IN_SUIT" |
| 457 | 98 if [ "$u" -eq 0 ] # Nested if/then condition test. |
| 458 | 99 then |
| 459 | 100 echo |
| 460 | 101 echo |
| 461 | 102 fi # Each hand set apart with a blank line. |
| 462 | 103 |
| 463 | 104 let "cards_picked += 1" |
| 464 | 105 fi |
| 465 | 106 done |
| 466 | 107 |
| 467 | 108 echo |
| 468 | 109 |
| 469 | 110 return 0 |
| 470 | 111 } |
| 471 | 112 |
| 472 | 113 |
| 473 | 114 # Structured programming: |
| 474 | 115 # Entire program logic modularized in functions. |
| 475 | 116 |
| 476 | 117 #=============== |
| 477 | 118 seed_random |
| 478 | 119 initialize_Deck |
| 479 | 120 initialize_Suits |
| 480 | 121 initialize_Cards |
| 481 | 122 deal_cards |
| 482 | 123 #=============== |
| 483 | 124 |
| 484 | 125 exit |
| 485 | 126 |
| 486 | 127 |
| 487 | 128 |
| 488 | 129 # Exercise 1: |
| 489 | 130 # Add comments to thoroughly document this script. |
| 490 | 131 |
| 491 | 132 # Exercise 2: |
| 492 | 133 # Add a routine (function) to print out each hand sorted in suits. |
| 493 | 134 # You may add other bells and whistles if you like. |
| 494 | 135 |
| 495 | 136 # Exercise 3: |
| 496 | 137 # 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 | > |