File: testbranch.html

package info (click to toggle)
abs-guide 10-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 6,952 kB
  • sloc: sh: 14,129; makefile: 81
file content (1081 lines) | stat: -rw-r--r-- 20,701 bytes parent folder | download | duplicates (3)
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&nbsp;#!/bin/bash
   2&nbsp;# Testing ranges of characters.
   3&nbsp;
   4&nbsp;echo; echo "Hit a key, then hit return."
   5&nbsp;read Keypress
   6&nbsp;
   7&nbsp;case "$Keypress" in
   8&nbsp;  [[:lower:]]   ) echo "Lowercase letter";;
   9&nbsp;  [[:upper:]]   ) echo "Uppercase letter";;
  10&nbsp;  [0-9]         ) echo "Digit";;
  11&nbsp;  *             ) echo "Punctuation, whitespace, or other";;
  12&nbsp;esac      #  Allows ranges of characters in [square brackets],
  13&nbsp;          #+ or POSIX ranges in [[double square brackets.
  14&nbsp;
  15&nbsp;#  In the first version of this example,
  16&nbsp;#+ the tests for lowercase and uppercase characters were
  17&nbsp;#+ [a-z] and [A-Z].
  18&nbsp;#  This no longer works in certain locales and/or Linux distros.
  19&nbsp;#  POSIX is more portable.
  20&nbsp;#  Thanks to Frank Wang for pointing this out.
  21&nbsp;
  22&nbsp;#  Exercise:
  23&nbsp;#  --------
  24&nbsp;#  As the script stands, it accepts a single keystroke, then terminates.
  25&nbsp;#  Change the script so it accepts repeated input,
  26&nbsp;#+ reports on each keystroke, and terminates only when "X" is hit.
  27&nbsp;#  Hint: enclose everything in a "while" loop.
  28&nbsp;
  29&nbsp;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&nbsp;#!/bin/bash
   2&nbsp;
   3&nbsp;# Crude address database
   4&nbsp;
   5&nbsp;clear # Clear the screen.
   6&nbsp;
   7&nbsp;echo "          Contact List"
   8&nbsp;echo "          ------- ----"
   9&nbsp;echo "Choose one of the following persons:" 
  10&nbsp;echo
  11&nbsp;echo "[E]vans, Roland"
  12&nbsp;echo "[J]ones, Mildred"
  13&nbsp;echo "[S]mith, Julie"
  14&nbsp;echo "[Z]ane, Morris"
  15&nbsp;echo
  16&nbsp;
  17&nbsp;read person
  18&nbsp;
  19&nbsp;case "$person" in
  20&nbsp;# Note variable is quoted.
  21&nbsp;
  22&nbsp;  "E" | "e" )
  23&nbsp;  # Accept upper or lowercase input.
  24&nbsp;  echo
  25&nbsp;  echo "Roland Evans"
  26&nbsp;  echo "4321 Flash Dr."
  27&nbsp;  echo "Hardscrabble, CO 80753"
  28&nbsp;  echo "(303) 734-9874"
  29&nbsp;  echo "(303) 734-9892 fax"
  30&nbsp;  echo "revans@zzy.net"
  31&nbsp;  echo "Business partner &#38; old friend"
  32&nbsp;  ;;
  33&nbsp;# Note double semicolon to terminate each option.
  34&nbsp;
  35&nbsp;  "J" | "j" )
  36&nbsp;  echo
  37&nbsp;  echo "Mildred Jones"
  38&nbsp;  echo "249 E. 7th St., Apt. 19"
  39&nbsp;  echo "New York, NY 10009"
  40&nbsp;  echo "(212) 533-2814"
  41&nbsp;  echo "(212) 533-9972 fax"
  42&nbsp;  echo "milliej@loisaida.com"
  43&nbsp;  echo "Ex-girlfriend"
  44&nbsp;  echo "Birthday: Feb. 11"
  45&nbsp;  ;;
  46&nbsp;
  47&nbsp;# Add info for Smith &#38; Zane later.
  48&nbsp;
  49&nbsp;          * )
  50&nbsp;   # Default option.	  
  51&nbsp;   # Empty input (hitting RETURN) fits here, too.
  52&nbsp;   echo
  53&nbsp;   echo "Not yet in database."
  54&nbsp;  ;;
  55&nbsp;
  56&nbsp;esac
  57&nbsp;
  58&nbsp;echo
  59&nbsp;
  60&nbsp;#  Exercise:
  61&nbsp;#  --------
  62&nbsp;#  Change the script so it accepts multiple inputs,
  63&nbsp;#+ instead of terminating after displaying just one address.
  64&nbsp;
  65&nbsp;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&nbsp;#! /bin/bash
   2&nbsp;
   3&nbsp;case "$1" in
   4&nbsp;  "") echo "Usage: ${0##*/} &#60;filename&#62;"; exit $E_PARAM;;
   5&nbsp;                      # No command-line parameters,
   6&nbsp;                      # or first parameter empty.
   7&nbsp;# Note that ${0##*/} is ${var##pattern} param substitution.
   8&nbsp;                      # Net result is $0.
   9&nbsp;
  10&nbsp;  -*) FILENAME=./$1;;   #  If filename passed as argument ($1)
  11&nbsp;                      #+ starts with a dash,
  12&nbsp;                      #+ replace it with ./$1
  13&nbsp;                      #+ so further commands don't interpret it
  14&nbsp;                      #+ as an option.
  15&nbsp;
  16&nbsp;  * ) FILENAME=$1;;     # Otherwise, $1.
  17&nbsp;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&nbsp;#! /bin/bash
   2&nbsp;
   3&nbsp;
   4&nbsp;while [ $# -gt 0 ]; do    # Until you run out of parameters . . .
   5&nbsp;  case "$1" in
   6&nbsp;    -d|--debug)
   7&nbsp;              # "-d" or "--debug" parameter?
   8&nbsp;              DEBUG=1
   9&nbsp;              ;;
  10&nbsp;    -c|--conf)
  11&nbsp;              CONFFILE="$2"
  12&nbsp;              shift
  13&nbsp;              if [ ! -f $CONFFILE ]; then
  14&nbsp;                echo "Error: Supplied file doesn't exist!"
  15&nbsp;                exit $E_CONFFILE     # File not found error.
  16&nbsp;              fi
  17&nbsp;              ;;
  18&nbsp;  esac
  19&nbsp;  shift       # Check next set of parameters.
  20&nbsp;done
  21&nbsp;
  22&nbsp;#  From Stefano Falsetto's "Log2Rot" script,
  23&nbsp;#+ part of his "rottlog" package.
  24&nbsp;#  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&nbsp;#!/bin/bash
   2&nbsp;# case-cmd.sh: Using command substitution to generate a "case" variable.
   3&nbsp;
   4&nbsp;case $( arch ) in   # $( arch ) returns machine architecture.
   5&nbsp;                    # Equivalent to 'uname -m' ...
   6&nbsp;  i386 ) echo "80386-based machine";;
   7&nbsp;  i486 ) echo "80486-based machine";;
   8&nbsp;  i586 ) echo "Pentium-based machine";;
   9&nbsp;  i686 ) echo "Pentium2+-based machine";;
  10&nbsp;  *    ) echo "Other type of machine";;
  11&nbsp;esac
  12&nbsp;
  13&nbsp;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&nbsp;#!/bin/bash
   2&nbsp;# match-string.sh: Simple string matching
   3&nbsp;#                  using a 'case' construct.
   4&nbsp;
   5&nbsp;match_string ()
   6&nbsp;{ # Exact string match.
   7&nbsp;  MATCH=0
   8&nbsp;  E_NOMATCH=90
   9&nbsp;  PARAMS=2     # Function requires 2 arguments.
  10&nbsp;  E_BAD_PARAMS=91
  11&nbsp;
  12&nbsp;  [ $# -eq $PARAMS ] || return $E_BAD_PARAMS
  13&nbsp;
  14&nbsp;  case "$1" in
  15&nbsp;  "$2") return $MATCH;;
  16&nbsp;  *   ) return $E_NOMATCH;;
  17&nbsp;  esac
  18&nbsp;
  19&nbsp;}  
  20&nbsp;
  21&nbsp;
  22&nbsp;a=one
  23&nbsp;b=two
  24&nbsp;c=three
  25&nbsp;d=two
  26&nbsp;
  27&nbsp;
  28&nbsp;match_string $a     # wrong number of parameters
  29&nbsp;echo $?             # 91
  30&nbsp;
  31&nbsp;match_string $a $b  # no match
  32&nbsp;echo $?             # 90
  33&nbsp;
  34&nbsp;match_string $b $d  # match
  35&nbsp;echo $?             # 0
  36&nbsp;
  37&nbsp;
  38&nbsp;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&nbsp;#!/bin/bash
   2&nbsp;# isalpha.sh: Using a "case" structure to filter a string.
   3&nbsp;
   4&nbsp;SUCCESS=0
   5&nbsp;FAILURE=1   #  Was FAILURE=-1,
   6&nbsp;            #+ but Bash no longer allows negative return value.
   7&nbsp;
   8&nbsp;isalpha ()  # Tests whether *first character* of input string is alphabetic.
   9&nbsp;{
  10&nbsp;if [ -z "$1" ]                # No argument passed?
  11&nbsp;then
  12&nbsp;  return $FAILURE
  13&nbsp;fi
  14&nbsp;
  15&nbsp;case "$1" in
  16&nbsp;  [a-zA-Z]*) return $SUCCESS;;  # Begins with a letter?
  17&nbsp;  *        ) return $FAILURE;;
  18&nbsp;esac
  19&nbsp;}             # Compare this with "isalpha ()" function in C.
  20&nbsp;
  21&nbsp;
  22&nbsp;isalpha2 ()   # Tests whether *entire string* is alphabetic.
  23&nbsp;{
  24&nbsp;  [ $# -eq 1 ] || return $FAILURE
  25&nbsp;
  26&nbsp;  case $1 in
  27&nbsp;  *[!a-zA-Z]*|"") return $FAILURE;;
  28&nbsp;               *) return $SUCCESS;;
  29&nbsp;  esac
  30&nbsp;}
  31&nbsp;
  32&nbsp;isdigit ()    # Tests whether *entire string* is numerical.
  33&nbsp;{             # In other words, tests for integer variable.
  34&nbsp;  [ $# -eq 1 ] || return $FAILURE
  35&nbsp;
  36&nbsp;  case $1 in
  37&nbsp;    *[!0-9]*|"") return $FAILURE;;
  38&nbsp;              *) return $SUCCESS;;
  39&nbsp;  esac
  40&nbsp;}
  41&nbsp;
  42&nbsp;
  43&nbsp;
  44&nbsp;check_var ()  # Front-end to isalpha ().
  45&nbsp;{
  46&nbsp;if isalpha "$@"
  47&nbsp;then
  48&nbsp;  echo "\"$*\" begins with an alpha character."
  49&nbsp;  if isalpha2 "$@"
  50&nbsp;  then        # No point in testing if first char is non-alpha.
  51&nbsp;    echo "\"$*\" contains only alpha characters."
  52&nbsp;  else
  53&nbsp;    echo "\"$*\" contains at least one non-alpha character."
  54&nbsp;  fi  
  55&nbsp;else
  56&nbsp;  echo "\"$*\" begins with a non-alpha character."
  57&nbsp;              # Also "non-alpha" if no argument passed.
  58&nbsp;fi
  59&nbsp;
  60&nbsp;echo
  61&nbsp;
  62&nbsp;}
  63&nbsp;
  64&nbsp;digit_check ()  # Front-end to isdigit ().
  65&nbsp;{
  66&nbsp;if isdigit "$@"
  67&nbsp;then
  68&nbsp;  echo "\"$*\" contains only digits [0 - 9]."
  69&nbsp;else
  70&nbsp;  echo "\"$*\" has at least one non-digit character."
  71&nbsp;fi
  72&nbsp;
  73&nbsp;echo
  74&nbsp;
  75&nbsp;}
  76&nbsp;
  77&nbsp;a=23skidoo
  78&nbsp;b=H3llo
  79&nbsp;c=-What?
  80&nbsp;d=What?
  81&nbsp;e=$(echo $b)   # Command substitution.
  82&nbsp;f=AbcDef
  83&nbsp;g=27234
  84&nbsp;h=27a34
  85&nbsp;i=27.34
  86&nbsp;
  87&nbsp;check_var $a
  88&nbsp;check_var $b
  89&nbsp;check_var $c
  90&nbsp;check_var $d
  91&nbsp;check_var $e
  92&nbsp;check_var $f
  93&nbsp;check_var     # No argument passed, so what happens?
  94&nbsp;#
  95&nbsp;digit_check $g
  96&nbsp;digit_check $h
  97&nbsp;digit_check $i
  98&nbsp;
  99&nbsp;
 100&nbsp;exit 0        # Script improved by S.C.
 101&nbsp;
 102&nbsp;# Exercise:
 103&nbsp;# --------
 104&nbsp;#  Write an 'isfloat ()' function that tests for floating point numbers.
 105&nbsp;#  Hint: The function duplicates 'isdigit ()',
 106&nbsp;#+ 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&nbsp;#!/bin/bash
   2&nbsp;
   3&nbsp;PS3='Choose your favorite vegetable: ' # Sets the prompt string.
   4&nbsp;                                       # Otherwise it defaults to #? .
   5&nbsp;
   6&nbsp;echo
   7&nbsp;
   8&nbsp;select vegetable in "beans" "carrots" "potatoes" "onions" "rutabagas"
   9&nbsp;do
  10&nbsp;  echo
  11&nbsp;  echo "Your favorite veggie is $vegetable."
  12&nbsp;  echo "Yuck!"
  13&nbsp;  echo
  14&nbsp;  break  # What happens if there is no 'break' here?
  15&nbsp;done
  16&nbsp;
  17&nbsp;exit
  18&nbsp;
  19&nbsp;# Exercise:
  20&nbsp;# --------
  21&nbsp;#  Fix this script to accept user input not specified in
  22&nbsp;#+ the "select" statement.
  23&nbsp;#  For example, if the user inputs "peas,"
  24&nbsp;#+ 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&nbsp;#!/bin/bash
   2&nbsp;
   3&nbsp;PS3='Choose your favorite vegetable: '
   4&nbsp;
   5&nbsp;echo
   6&nbsp;
   7&nbsp;choice_of()
   8&nbsp;{
   9&nbsp;select vegetable
  10&nbsp;# [in list] omitted, so 'select' uses arguments passed to function.
  11&nbsp;do
  12&nbsp;  echo
  13&nbsp;  echo "Your favorite veggie is $vegetable."
  14&nbsp;  echo "Yuck!"
  15&nbsp;  echo
  16&nbsp;  break
  17&nbsp;done
  18&nbsp;}
  19&nbsp;
  20&nbsp;choice_of beans rice carrots radishes rutabaga spinach
  21&nbsp;#         $1    $2   $3      $4       $5       $6
  22&nbsp;#         passed to choice_of() function
  23&nbsp;
  24&nbsp;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&nbsp;case $( arch ) in   # $( arch ) returns machine architecture.
   2&nbsp;  ( i386 ) echo "80386-based machine";;
   3&nbsp;# ^      ^
   4&nbsp;  ( i486 ) echo "80486-based machine";;
   5&nbsp;  ( i586 ) echo "Pentium-based machine";;
   6&nbsp;  ( i686 ) echo "Pentium2+-based machine";;
   7&nbsp;  (    * ) echo "Other type of machine";;
   8&nbsp;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
>