10.4. ²âÊÔÓë·ÖÖ§(caseÓëselect½á¹¹)

caseºÍselect½á¹¹ÔÚ¼¼ÊõÉÏ˵²¢²»ÊÇÑ­»·, ÒòΪËüÃDz¢²»¶Ô¿ÉÖ´ÐдúÂë¿é½øÐеü´ú. µ«ÊǺÍÑ­»·ÏàËƵÄÊÇ, ËüÃÇÒ²ÒÀ¿¿ÔÚ´úÂë¿é¶¥²¿»òµ×²¿µÄÌõ¼þÅжÏÀ´¾ö¶¨³ÌÐòµÄ·ÖÖ§.

ÔÚ´úÂë¿éÖпØÖƳÌÐò·ÖÖ§

case (in) / esac

ÔÚshellÖеÄcase½á¹¹ÓëC/C++ÖеÄswitch½á¹¹ÊÇÏàͬµÄ. ËüÔÊÐíͨ¹ýÅжÏÀ´Ñ¡Ôñ´úÂë¿éÖжàÌõ·¾¶ÖеÄÒ»Ìõ. ËüµÄ×÷ÓúͶà¸öif/then/elseÓï¾äµÄ×÷ÓÃÏàͬ, ÊÇËüÃǵļò»¯½á¹¹, ÌرðÊÊÓÃÓÚ´´½¨²Ëµ¥.

case "$variable" in

 "$condition1" )
 command...
 ;;

 "$condition2" )
 command...
 ;;

esac

Note

  • ¶Ô±äÁ¿Ê¹ÓÃ""²¢²»ÊÇÇ¿ÖƵÄ, ÒòΪ²»»á·¢Éúµ¥´Ê·Ö¸î.

  • ÿ¾ä²âÊÔÐÐ, ¶¼ÒÔÓÒСÀ¨ºÅ)À´½áβ.

  • ÿ¸öÌõ¼þÅжÏÓï¾ä¿é¶¼ÒÔÒ»¶Ô·ÖºÅ½áβ ;;.

  • case¿éÒÔesac (caseµÄ·´Ïòƴд)½áβ.


Àý×Ó 10-24. ʹÓÃcase

  1 #!/bin/bash
  2 # ²âÊÔ×Ö·û´®·¶Î§.
  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      #  ÔÊÐí×Ö·û´®µÄ·¶Î§³öÏÖÔÚ[ÖÐÀ¨ºÅ]ÖÐ,
 13           #+ »òÕß³öÏÖÔÚPOSIX·ç¸ñµÄ[[Ë«ÖÐÀ¨ºÅÖÐ.
 14 
 15 #  ÔÚÕâ¸öÀý×ӵĵÚÒ»¸ö°æ±¾ÖÐ,
 16 #+ ²âÊÔ´óдºÍСд×Ö·û´®µÄ¹¤×÷ʹÓõÄÊÇ
 17 #+ [a-z] ºÍ [A-Z].
 18 #  ÕâÖÖÓ÷¨ÔÚijЩÌض¨³¡ºÏµÄ»òijЩLinux·¢ÐаæÖв»Äܹ»Õý³£¹¤×÷.
 19 #  POSIX µÄ·ç¸ñ¸ü¾ß¿ÉÒÆÖ²ÐÔ.
 20 #  ¸ÐлFrank WangÖ¸³öÁËÕâµã.
 21 
 22 #  Á·Ï°:
 23 #  -----
 24 #  ¾ÍÏñÕâ¸ö½Å±¾Ëù±íÏÖ³öÀ´µÄ, ËüÖ»ÔÊÐíµ¥´ÎµÄ°´¼ü, È»ºó¾Í½áÊøÁË.
 25 #  ÐÞ¸ÄÕâ¸ö½Å±¾, ÈÃËüÄܹ»½ÓÊÜÖظ´ÊäÈë,
 26 #+ ±¨¸æÿ´Î°´¼ü, ²¢ÇÒÖ»ÓÐÔÚ"X"±»¼üÈëʱ²Å½áÊø. 
 27 #  °µÊ¾: ½«ÕâЩ´úÂ붼ÓÃ"while"Ñ­»·È¦ÆðÀ´. 
 28 
 29 exit 0


Àý×Ó 10-25. ʹÓÃcaseÀ´´´½¨²Ëµ¥

  1 #!/bin/bash
  2 
  3 # δ¾­´¦ÀíµÄµØÖ·×ÊÁÏ
  4 
  5 clear # ÇåÆÁ.
  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 # ×¢Òâ, ±äÁ¿ÊDZ»""ÒýÓõÄ.
 21 
 22   "E" | "e" )
 23   # ½ÓÊÜ´óд»òÕßСдÊäÈë.
 24   echo
 25   echo "Roland Evans"
 26   echo "4321 Floppy 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 # ×¢Òâ, ÿ¸öÑ¡Ïîºó±ß¶¼ÒªÒÔË«·ÖºÅ;;½áβ.
 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 # ºó±ßµÄ Smith ºÍ Zane µÄÐÅÏ¢ÔÚÕâÀï¾ÍÊ¡ÂÔÁË.
 48 
 49           * )
 50    # ĬÈÏÑ¡Ïî.
 51    # ¿ÕÊäÈë(ÇûسµRETURN), Ò²ÊÊÓÃÓÚÕâÀï. 
 52    echo
 53    echo "Not yet in database."
 54   ;;
 55 
 56 esac
 57 
 58 echo
 59 
 60 #  Á·Ï°:
 61 #  -----
 62 #  ÐÞ¸ÄÕâ¸ö½Å±¾, ÈÃËüÄܹ»½ÓÊܶà¸öÊäÈë,
 63 #+ ²¢ÇÒÄܹ»ÏÔʾ¶à¸öµØÖ·.
 64 
 65 exit 0

Ò»¸öcaseµÄ·Ç³£´ÏÃ÷µÄÓ÷¨, ÓÃÀ´²âÊÔÃüÁîÐвÎÊý.
  1 #! /bin/bash
  2 
  3 case "$1" in
  4 "") echo "Usage: ${0##*/} <filename>"; exit $E_PARAM;;  # ûÓÐÃüÁîÐвÎÊý,
  5                                                         # »òÕßµÚÒ»¸ö²ÎÊýΪ¿Õ.
  6 # ×¢Òâ: ${0##*/} ÊÇ ${var##pattern} µÄÒ»ÖÖÌæ»»ÐÎʽ. µÃµ½µÄ½á¹ûΪ$0.
  7 
  8 -*) FILENAME=./$1;;   #  Èç¹û´«µÝ½øÀ´µÄÎļþÃû²ÎÊý($1)ÒÔÒ»¸öÆÆÕۺſªÍ·, 
  9                       #+ ÄÇôÓÃ./$1À´´úÌæ.
 10                       #+ ÕâÑùºó±ßµÄÃüÁ²»»á°ÑËü×÷Ϊһ¸öÑ¡ÏîÀ´½âÊÍ.
 11 
 12 * ) FILENAME=$1;;     # ·ñÔò, $1.
 13 esac

ÕâÊÇÒ»¸öÃüÁîÐвÎÊý´¦ÀíµÄ¸üÈÝÒ×Àí½âµÄÀý×Ó:
  1 #! /bin/bash
  2 
  3 
  4 while [ $# -gt 0 ]; do    # Ö±µ½ÄãÓÃÍêËùÓеIJÎÊý . . .
  5   case "$1" in
  6     -d|--debug)
  7               # ÊÇ "-d" »ò "--debug" ²ÎÊý?
  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     # ´íÎó: Îļþδ·¢ÏÖ.
 16               fi
 17               ;;
 18   esac
 19   shift       # ¼ì²éÊ£ÓàµÄ²ÎÊý.
 20 done
 21 
 22 #  À´×ÔStefano FalsettoµÄ "Log2Rot" ½Å±¾,
 23 #+ ²¢ÇÒÊÇËûµÄ"rottlog"°üµÄÒ»²¿·Ö. 
 24 #  Òѵõ½Ê¹ÓÃÐí¿É. 


Àý×Ó 10-26. ʹÓÃÃüÁîÌæ»»À´²úÉúcase±äÁ¿

  1 #!/bin/bash
  2 # case-cmd.sh: ʹÓÃÃüÁîÌæ»»À´²úÉú"case"±äÁ¿.
  3 
  4 case $( arch ) in   # "arch" ·µ»Ø»úÆ÷ÌåϵµÄÀàÐÍ.
  5                     # µÈ¼ÛÓÚ '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

case½á¹¹Ò²¿ÉÒÔ¹ýÂËͨÅä(globbing)ģʽµÄ×Ö·û´®.


Àý×Ó 10-27. ¼òµ¥µÄ×Ö·û´®Æ¥Åä

  1 #!/bin/bash
  2 # match-string.sh: ¼òµ¥µÄ×Ö·û´®Æ¥Åä
  3 
  4 match_string ()
  5 {
  6   MATCH=0
  7   NOMATCH=90
  8   PARAMS=2     # ´Ëº¯ÊýÐèÒª2¸ö²ÎÊý.
  9   BAD_PARAMS=91
 10 
 11   [ $# -eq $PARAMS ] || return $BAD_PARAMS
 12 
 13   case "$1" in
 14   "$2") return $MATCH;;
 15   *   ) return $NOMATCH;;
 16   esac
 17 
 18 }  
 19 
 20 
 21 a=one
 22 b=two
 23 c=three
 24 d=two
 25 
 26 
 27 match_string $a     # ²ÎÊý¸öÊý´íÎó.
 28 echo $?             # 91
 29 
 30 match_string $a $b  # ²»Æ¥Åä
 31 echo $?             # 90
 32 
 33 match_string $b $d  # Æ¥Åä
 34 echo $?             # 0
 35 
 36 
 37 exit 0		    


Àý×Ó 10-28. ¼ì²éÊäÈë×Ö·ûÊÇ·ñΪ×Öĸ

  1 #!/bin/bash
  2 # isalpha.sh: ʹÓÃ"case"½á¹¹À´¹ýÂË×Ö·û´®. 
  3 
  4 SUCCESS=0
  5 FAILURE=-1
  6 
  7 isalpha ()  # ¼ì²éÊäÈëµÄ *µÚÒ»¸ö×Ö·û* ÊDz»ÊÇ×Öĸ±íÉϵÄ×Ö·û.
  8 {
  9 if [ -z "$1" ]                # ûÓвÎÊý´«½øÀ´?
 10 then
 11   return $FAILURE
 12 fi
 13 
 14 case "$1" in
 15 [a-zA-Z]*) return $SUCCESS;;  # ÒÔÒ»¸ö×Öĸ¿ªÍ·?
 16 *        ) return $FAILURE;;
 17 esac
 18 }             # ͬCÓïÑÔµÄ"isalpha ()"º¯Êý±È½ÏÒ»ÏÂ. 
 19 
 20 
 21 isalpha2 ()   # ²âÊÔ *Õû¸ö×Ö·û´®* ÊÇ·ñ¶¼ÊÇ×Öĸ±íÉϵÄ×Ö·û.
 22 {
 23   [ $# -eq 1 ] || return $FAILURE
 24 
 25   case $1 in
 26   *[!a-zA-Z]*|"") return $FAILURE;;
 27                *) return $SUCCESS;;
 28   esac
 29 }
 30 
 31 isdigit ()    # ²âÊÔ *Õû¸ö×Ö·û´®* ÊÇ·ñ¶¼ÊÇÊý×Ö.
 32 {             # »»¾ä»°Ëµ, ¾ÍÊDzâÊÔÒ»ÏÂÊÇ·ñÊÇÕûÊý±äÁ¿.
 33   [ $# -eq 1 ] || return $FAILURE
 34 
 35   case $1 in
 36   *[!0-9]*|"") return $FAILURE;;
 37             *) return $SUCCESS;;
 38   esac
 39 }
 40 
 41 
 42 
 43 check_var ()  # ²âÊÔisalpha().
 44 {
 45 if isalpha "$@"
 46 then
 47   echo "\"$*\" begins with an alpha character."
 48   if isalpha2 "$@"
 49   then        # ²»ÐèÒª²âÊÔµÚÒ»¸ö×Ö·ûÊÇ·ñÊÇnon-alpha.
 50     echo "\"$*\" contains only alpha characters."
 51   else
 52     echo "\"$*\" contains at least one non-alpha character."
 53   fi  
 54 else
 55   echo "\"$*\" begins with a non-alpha character."
 56               # Èç¹ûûÓвÎÊý´«µÝ½øÀ´, Ò²ÊÇ"non-alpha". 
 57 fi
 58 
 59 echo
 60 
 61 }
 62 
 63 digit_check ()  # ²âÊÔisdigit().
 64 {
 65 if isdigit "$@"
 66 then
 67   echo "\"$*\" contains only digits [0 - 9]."
 68 else
 69   echo "\"$*\" has at least one non-digit character."
 70 fi
 71 
 72 echo
 73 
 74 }
 75 
 76 a=23skidoo
 77 b=H3llo
 78 c=-What?
 79 d=What?
 80 e=`echo $b`   # ÃüÁîÌæ»».
 81 f=AbcDef
 82 g=27234
 83 h=27a34
 84 i=27.34
 85 
 86 check_var $a
 87 check_var $b
 88 check_var $c
 89 check_var $d
 90 check_var $e
 91 check_var $f
 92 check_var     # ûÓвÎÊý´«µÝ½øÀ´, ½«»á·¢Éúʲô?
 93 #
 94 digit_check $g
 95 digit_check $h
 96 digit_check $i
 97 
 98 
 99 exit 0        # S.C¸Ä½øÁËÕâ¸ö½Å±¾.
100 
101 # Á·Ï°:
102 # -----
103 #  ±àдһ¸ö'isfloat ()'º¯ÊýÀ´²âÊÔ¸¡µãÊý.
104 #  °µÊ¾: Õâ¸öº¯Êý»ù±¾ÉÏÓë'isdigit ()'Ïàͬ,
105 #+ µ«ÊÇÒªÌí¼ÓһЩСÊýµã²¿·ÖµÄ´¦Àí.

select

select½á¹¹Êǽ¨Á¢²Ëµ¥µÄÁíÒ»ÖÖ¹¤¾ß, ÕâÖֽṹÊÇ´ÓkshÖÐÒýÈëµÄ.

select variable [in list]
do
 command...
 break
done

ÌáʾÓû§ÊäÈëÑ¡ÔñµÄÄÚÈÝ(±ÈÈç·ÅÔÚ±äÁ¿ÁбíÖÐ). ×¢Òâ: selectÃüÁîʹÓÃPS3Ìáʾ·û, ĬÈÏΪ(#?), µ±È», Õâ¿ÉÒÔÐÞ¸Ä.


Àý×Ó 10-29. ʹÓÃselectÀ´´´½¨²Ëµ¥

  1 #!/bin/bash
  2 
  3 PS3='Choose your favorite vegetable: ' # ÉèÖÃÌáʾ·û×Ö´®.
  4 
  5 echo
  6 
  7 select vegetable in "beans" "carrots" "potatoes" "onions" "rutabagas"
  8 do
  9   echo
 10   echo "Your favorite veggie is $vegetable."
 11   echo "Yuck!"
 12   echo
 13   break  # Èç¹ûÕâÀïûÓÐ 'break' »á·¢Éúʲô?
 14 done
 15 
 16 exit 0

Èç¹ûºöÂÔÁËin listÁбí, ÄÇôselectÃüÁ»áʹÓô«µÝµ½½Å±¾µÄÃüÁîÐвÎÊý($@), »òÕßÊǺ¯Êý²ÎÊý(µ±selectÊÇÔÚº¯ÊýÖÐʱ).

ÓëºöÂÔin listµÄ

for variable [in list]

½á¹¹±È½ÏÒ»ÏÂ.


Àý×Ó 10-30. ʹÓú¯ÊýÖеÄselect½á¹¹À´´´½¨²Ëµ¥

  1 #!/bin/bash
  2 
  3 PS3='Choose your favorite vegetable: '
  4 
  5 echo
  6 
  7 choice_of()
  8 {
  9 select vegetable
 10 # [in list]±»ºöÂÔ, ËùÒÔ'select'ʹÓô«µÝ¸øº¯ÊýµÄ²ÎÊý.
 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 tomatoes spinach
 21 #         $1    $2   $3      $4       $5       $6
 22 #         ´«µÝ¸øchoice_of()µÄ²ÎÊý
 23 
 24 exit 0

²Î¿¼Àý×Ó 34-3.