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 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Testing and Branching</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="Loops and Branches"
HREF="loops.html"><LINK
REL="PREVIOUS"
TITLE="Loop Control"
HREF="loopcontrol.html"><LINK
REL="NEXT"
TITLE="Command Substitution"
HREF="commandsub.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="SECT1"
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="loopcontrol.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 11. Loops and Branches</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="commandsub.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="TESTBRANCH"
></A
>11.4. Testing and Branching</H1
><P
>The <B
CLASS="COMMAND"
>case</B
> and <B
CLASS="COMMAND"
>select</B
>
constructs are technically not loops, since they do not iterate the
execution of a code block. Like loops, however, they direct
program flow according to conditions at the top or bottom of
the block.</P
><DIV
CLASS="VARIABLELIST"
><P
><B
><A
NAME="CASEESAC1"
></A
>Controlling program flow in a code
block</B
></P
><DL
><DT
><B
CLASS="COMMAND"
>case (in) / esac</B
></DT
><DD
><P
>The <B
CLASS="COMMAND"
>case</B
> construct is the shell
scripting analog to <TT
CLASS="REPLACEABLE"
><I
>switch</I
></TT
>
in <B
CLASS="COMMAND"
>C/C++</B
>.
It permits branching to one of a number of code blocks,
depending on condition tests. It serves as a kind of
shorthand for multiple <SPAN
CLASS="TOKEN"
>if/then/else</SPAN
>
statements and is an appropriate tool for creating
menus.</P
><P
><P
><B
CLASS="COMMAND"
>case</B
> "$<TT
CLASS="REPLACEABLE"
><I
>variable</I
></TT
>" in <BR><BR> "$<TT
CLASS="REPLACEABLE"
><I
>condition1</I
></TT
>" ) <BR> <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
>... <BR> ;; <BR><BR> "$<TT
CLASS="REPLACEABLE"
><I
>condition2</I
></TT
>" ) <BR> <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
>... <BR> ;; <BR><BR><BR><B
CLASS="COMMAND"
>esac</B
> </P
></P
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
> <UL
><LI
><P
>Quoting the variables is not mandatory,
since word splitting does not take place.</P
></LI
><LI
><P
><A
NAME="CASEPAREN"
></A
>Each test line
ends with a right paren <B
CLASS="COMMAND"
>)</B
>.
<A
NAME="AEN7087"
HREF="#FTN.AEN7087"
>[1]</A
>
</P
></LI
><LI
><P
>Each condition block ends
with a <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>double</I
></SPAN
> semicolon
<SPAN
CLASS="TOKEN"
>;;</SPAN
>.</P
></LI
><LI
><P
>If a condition tests
<I
CLASS="FIRSTTERM"
>true</I
>, then the associated
commands execute and the <B
CLASS="COMMAND"
>case</B
>
block terminates.</P
></LI
><LI
><P
>The entire <B
CLASS="COMMAND"
>case</B
>
block ends with an <B
CLASS="COMMAND"
>esac</B
>
(<I
CLASS="WORDASWORD"
>case</I
> spelled backwards).</P
></LI
></UL
>
</P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX29"
></A
><P
><B
>Example 11-25. Using <I
CLASS="FIRSTTERM"
>case</I
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # Testing ranges of characters.
3
4 echo; echo "Hit a key, then hit return."
5 read Keypress
6
7 case "$Keypress" in
8 [[:lower:]] ) echo "Lowercase letter";;
9 [[:upper:]] ) echo "Uppercase letter";;
10 [0-9] ) echo "Digit";;
11 * ) echo "Punctuation, whitespace, or other";;
12 esac # Allows ranges of characters in [square brackets],
13 #+ or POSIX ranges in [[double square brackets.
14
15 # In the first version of this example,
16 #+ the tests for lowercase and uppercase characters were
17 #+ [a-z] and [A-Z].
18 # This no longer works in certain locales and/or Linux distros.
19 # POSIX is more portable.
20 # Thanks to Frank Wang for pointing this out.
21
22 # Exercise:
23 # --------
24 # As the script stands, it accepts a single keystroke, then terminates.
25 # Change the script so it accepts repeated input,
26 #+ reports on each keystroke, and terminates only when "X" is hit.
27 # Hint: enclose everything in a "while" loop.
28
29 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX30"
></A
><P
><B
>Example 11-26. Creating menus using <I
CLASS="FIRSTTERM"
>case</I
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 # Crude address database
4
5 clear # Clear the screen.
6
7 echo " Contact List"
8 echo " ------- ----"
9 echo "Choose one of the following persons:"
10 echo
11 echo "[E]vans, Roland"
12 echo "[J]ones, Mildred"
13 echo "[S]mith, Julie"
14 echo "[Z]ane, Morris"
15 echo
16
17 read person
18
19 case "$person" in
20 # Note variable is quoted.
21
22 "E" | "e" )
23 # Accept upper or lowercase input.
24 echo
25 echo "Roland Evans"
26 echo "4321 Flash Dr."
27 echo "Hardscrabble, CO 80753"
28 echo "(303) 734-9874"
29 echo "(303) 734-9892 fax"
30 echo "revans@zzy.net"
31 echo "Business partner & old friend"
32 ;;
33 # Note double semicolon to terminate each option.
34
35 "J" | "j" )
36 echo
37 echo "Mildred Jones"
38 echo "249 E. 7th St., Apt. 19"
39 echo "New York, NY 10009"
40 echo "(212) 533-2814"
41 echo "(212) 533-9972 fax"
42 echo "milliej@loisaida.com"
43 echo "Ex-girlfriend"
44 echo "Birthday: Feb. 11"
45 ;;
46
47 # Add info for Smith & Zane later.
48
49 * )
50 # Default option.
51 # Empty input (hitting RETURN) fits here, too.
52 echo
53 echo "Not yet in database."
54 ;;
55
56 esac
57
58 echo
59
60 # Exercise:
61 # --------
62 # Change the script so it accepts multiple inputs,
63 #+ instead of terminating after displaying just one address.
64
65 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
><A
NAME="CASECL"
></A
></P
><P
>An exceptionally clever use of <B
CLASS="COMMAND"
>case</B
>
involves testing for command-line parameters.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #! /bin/bash
2
3 case "$1" in
4 "") echo "Usage: ${0##*/} <filename>"; exit $E_PARAM;;
5 # No command-line parameters,
6 # or first parameter empty.
7 # Note that ${0##*/} is ${var##pattern} param substitution.
8 # Net result is $0.
9
10 -*) FILENAME=./$1;; # If filename passed as argument ($1)
11 #+ starts with a dash,
12 #+ replace it with ./$1
13 #+ so further commands don't interpret it
14 #+ as an option.
15
16 * ) FILENAME=$1;; # Otherwise, $1.
17 esac</PRE
></TD
></TR
></TABLE
></P
><P
>Here is a more straightforward example of
command-line parameter handling:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #! /bin/bash
2
3
4 while [ $# -gt 0 ]; do # Until you run out of parameters . . .
5 case "$1" in
6 -d|--debug)
7 # "-d" or "--debug" parameter?
8 DEBUG=1
9 ;;
10 -c|--conf)
11 CONFFILE="$2"
12 shift
13 if [ ! -f $CONFFILE ]; then
14 echo "Error: Supplied file doesn't exist!"
15 exit $E_CONFFILE # File not found error.
16 fi
17 ;;
18 esac
19 shift # Check next set of parameters.
20 done
21
22 # From Stefano Falsetto's "Log2Rot" script,
23 #+ part of his "rottlog" package.
24 # Used with permission.</PRE
></TD
></TR
></TABLE
></P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="CASECMD"
></A
><P
><B
>Example 11-27. Using <I
CLASS="FIRSTTERM"
>command substitution</I
>
to generate the <I
CLASS="FIRSTTERM"
>case</I
> variable</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # case-cmd.sh: Using command substitution to generate a "case" variable.
3
4 case $( arch ) in # $( arch ) returns machine architecture.
5 # Equivalent to 'uname -m' ...
6 i386 ) echo "80386-based machine";;
7 i486 ) echo "80486-based machine";;
8 i586 ) echo "Pentium-based machine";;
9 i686 ) echo "Pentium2+-based machine";;
10 * ) echo "Other type of machine";;
11 esac
12
13 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
><A
NAME="CSGLOB"
></A
></P
><P
>A <B
CLASS="COMMAND"
>case</B
> construct can filter strings for
<A
HREF="globbingref.html"
>globbing</A
> patterns.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="MATCHSTRING"
></A
><P
><B
>Example 11-28. Simple string matching</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # match-string.sh: Simple string matching
3 # using a 'case' construct.
4
5 match_string ()
6 { # Exact string match.
7 MATCH=0
8 E_NOMATCH=90
9 PARAMS=2 # Function requires 2 arguments.
10 E_BAD_PARAMS=91
11
12 [ $# -eq $PARAMS ] || return $E_BAD_PARAMS
13
14 case "$1" in
15 "$2") return $MATCH;;
16 * ) return $E_NOMATCH;;
17 esac
18
19 }
20
21
22 a=one
23 b=two
24 c=three
25 d=two
26
27
28 match_string $a # wrong number of parameters
29 echo $? # 91
30
31 match_string $a $b # no match
32 echo $? # 90
33
34 match_string $b $d # match
35 echo $? # 0
36
37
38 exit 0 </PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="ISALPHA"
></A
><P
><B
>Example 11-29. Checking for alphabetic input</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # isalpha.sh: Using a "case" structure to filter a string.
3
4 SUCCESS=0
5 FAILURE=1 # Was FAILURE=-1,
6 #+ but Bash no longer allows negative return value.
7
8 isalpha () # Tests whether *first character* of input string is alphabetic.
9 {
10 if [ -z "$1" ] # No argument passed?
11 then
12 return $FAILURE
13 fi
14
15 case "$1" in
16 [a-zA-Z]*) return $SUCCESS;; # Begins with a letter?
17 * ) return $FAILURE;;
18 esac
19 } # Compare this with "isalpha ()" function in C.
20
21
22 isalpha2 () # Tests whether *entire string* is alphabetic.
23 {
24 [ $# -eq 1 ] || return $FAILURE
25
26 case $1 in
27 *[!a-zA-Z]*|"") return $FAILURE;;
28 *) return $SUCCESS;;
29 esac
30 }
31
32 isdigit () # Tests whether *entire string* is numerical.
33 { # In other words, tests for integer variable.
34 [ $# -eq 1 ] || return $FAILURE
35
36 case $1 in
37 *[!0-9]*|"") return $FAILURE;;
38 *) return $SUCCESS;;
39 esac
40 }
41
42
43
44 check_var () # Front-end to isalpha ().
45 {
46 if isalpha "$@"
47 then
48 echo "\"$*\" begins with an alpha character."
49 if isalpha2 "$@"
50 then # No point in testing if first char is non-alpha.
51 echo "\"$*\" contains only alpha characters."
52 else
53 echo "\"$*\" contains at least one non-alpha character."
54 fi
55 else
56 echo "\"$*\" begins with a non-alpha character."
57 # Also "non-alpha" if no argument passed.
58 fi
59
60 echo
61
62 }
63
64 digit_check () # Front-end to isdigit ().
65 {
66 if isdigit "$@"
67 then
68 echo "\"$*\" contains only digits [0 - 9]."
69 else
70 echo "\"$*\" has at least one non-digit character."
71 fi
72
73 echo
74
75 }
76
77 a=23skidoo
78 b=H3llo
79 c=-What?
80 d=What?
81 e=$(echo $b) # Command substitution.
82 f=AbcDef
83 g=27234
84 h=27a34
85 i=27.34
86
87 check_var $a
88 check_var $b
89 check_var $c
90 check_var $d
91 check_var $e
92 check_var $f
93 check_var # No argument passed, so what happens?
94 #
95 digit_check $g
96 digit_check $h
97 digit_check $i
98
99
100 exit 0 # Script improved by S.C.
101
102 # Exercise:
103 # --------
104 # Write an 'isfloat ()' function that tests for floating point numbers.
105 # Hint: The function duplicates 'isdigit ()',
106 #+ but adds a test for a mandatory decimal point.</PRE
></TD
></TR
></TABLE
><HR></DIV
></DD
><DT
><A
NAME="SELECTREF"
></A
><B
CLASS="COMMAND"
>select</B
></DT
><DD
><P
>The <B
CLASS="COMMAND"
>select</B
> construct, adopted from the Korn
Shell, is yet another tool for building menus.</P
><P
><P
><B
CLASS="COMMAND"
>select</B
> <TT
CLASS="REPLACEABLE"
><I
>variable</I
></TT
> [in <TT
CLASS="REPLACEABLE"
><I
>list</I
></TT
>]<BR> do <BR> <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
>... <BR> break <BR> done </P
></P
><P
>This prompts the user to enter one of the choices presented in the
variable list. Note that <B
CLASS="COMMAND"
>select</B
> uses the
<TT
CLASS="VARNAME"
>$PS3</TT
> prompt (<TT
CLASS="PROMPT"
>#? </TT
>) by default,
but this may be changed.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX31"
></A
><P
><B
>Example 11-30. Creating menus using <I
CLASS="FIRSTTERM"
>select</I
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 PS3='Choose your favorite vegetable: ' # Sets the prompt string.
4 # Otherwise it defaults to #? .
5
6 echo
7
8 select vegetable in "beans" "carrots" "potatoes" "onions" "rutabagas"
9 do
10 echo
11 echo "Your favorite veggie is $vegetable."
12 echo "Yuck!"
13 echo
14 break # What happens if there is no 'break' here?
15 done
16
17 exit
18
19 # Exercise:
20 # --------
21 # Fix this script to accept user input not specified in
22 #+ the "select" statement.
23 # For example, if the user inputs "peas,"
24 #+ the script would respond "Sorry. That is not on the menu."</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
><A
NAME="INLISTOMIT"
></A
></P
><P
>If <TT
CLASS="USERINPUT"
><B
>in <TT
CLASS="REPLACEABLE"
><I
>list</I
></TT
></B
></TT
> is
omitted, then <B
CLASS="COMMAND"
>select</B
> uses the list of command
line arguments (<TT
CLASS="VARNAME"
>$@</TT
>) passed to the script or
the function containing the <B
CLASS="COMMAND"
>select</B
>
construct.</P
><P
>Compare this to the behavior of a
<P
><B
CLASS="COMMAND"
>for</B
> <TT
CLASS="REPLACEABLE"
><I
>variable</I
></TT
> [in <TT
CLASS="REPLACEABLE"
><I
>list</I
></TT
>]</P
>
construct with the
<TT
CLASS="USERINPUT"
><B
>in <TT
CLASS="REPLACEABLE"
><I
>list</I
></TT
></B
></TT
>
omitted.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX32"
></A
><P
><B
>Example 11-31. Creating menus using <I
CLASS="FIRSTTERM"
>select</I
>
in a function</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 PS3='Choose your favorite vegetable: '
4
5 echo
6
7 choice_of()
8 {
9 select vegetable
10 # [in list] omitted, so 'select' uses arguments passed to function.
11 do
12 echo
13 echo "Your favorite veggie is $vegetable."
14 echo "Yuck!"
15 echo
16 break
17 done
18 }
19
20 choice_of beans rice carrots radishes rutabaga spinach
21 # $1 $2 $3 $4 $5 $6
22 # passed to choice_of() function
23
24 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>See also <A
HREF="bash2.html#RESISTOR"
>Example 37-3</A
>.</P
></DD
></DL
></DIV
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN7087"
HREF="testbranch.html#AEN7087"
>[1]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>Pattern-match lines may also <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>start</I
></SPAN
>
with a <B
CLASS="COMMAND"
>(</B
> left paren to give the layout
a more structured appearance.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 case $( arch ) in # $( arch ) returns machine architecture.
2 ( i386 ) echo "80386-based machine";;
3 # ^ ^
4 ( i486 ) echo "80486-based machine";;
5 ( i586 ) echo "Pentium-based machine";;
6 ( i686 ) echo "Pentium2+-based machine";;
7 ( * ) echo "Other type of machine";;
8 esac</PRE
></TD
></TR
></TABLE
></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="loopcontrol.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="commandsub.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Loop Control</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="loops.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Command Substitution</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|