case的格式

 
  
  1. case 值 in 
  2. 模式1) 
  3.     命令1 
  4.       ;; 
  5. 模式2) 
  6.     命令2 
  7.       ;; 
  8. esac 

case取值后面必须为单词in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至;;。模式匹配符*表示任意字符,?表示任意单字符,[..]表示类或范围中任意字符。

例子如下:

 
  
  1. #!/bin/bash 
  2. echo -n "Please input number between 1 to 3 or a to c:" 
  3. read number 
  4. case $number in 
  5. 1) 
  6. echo "your choice is $number" 
  7. ;;//看到了每行后面都要有两个分号做为结束 
  8. 2) 
  9. echo "your choice is $number" 
  10. ;; 
  11. 3) 
  12. echo "your choice is $number" 
  13. ;; 
  14. a) 
  15. echo "$[1+2]"//进行和的运算 
  16. ;; 
  17. ?)//表示任意单个字符 
  18. echo "your input is single $number" 
  19. ;; 
  20. *)//表示任意多个字符 
  21. echo "your choice are $number,'basename $0':This is not between 1 to 3 or a to c" 
  22. exit 
  23. ;; 
  24. esac//要请注意这个结束符和if的一样,要反写 

就这些吧,这些例子足以说明case的用法了。