case statement
- format
case variable in value 1) performs an operation ;; value 2) perform actions 2 ;; value 3) perform actions 3 ;; .... * ) If the value of a variable value or more is not, this procedure is performed ;; Esac
-
-
Conventional break is added next to each entry ;;
-
Unconditional continues to increase after each one; &
-
Conditional continue it every behind the increase ;; &
- example
1 #!/bin/bash 2 3 num=1 4 case $num in 5 1 ) 6 echo "1" 7 ;; 8 2 ) 9 echo "2" 10 ;; 11 3 ) 12 echo "3" 13 ;; 14 * ) 15 echo "not 1 2 3" 16 ;; 17 esac 18 # Output: . 1 . 19 20 is ! # / Bin / the bash 21 is 22 is NUM = . 1 23 is Case $ NUM in 24 . 1 ) 25 echo " . 1 " 26 is ;; & # rearwardly matching conditions 27 2 ) 28 echo " 2 " 29 ;; 30 . 3 ) 31 is echo " . 3 " 32 ;; 33 is * ) 34 is echo "2. 3. 1 Not " 35 ;; 36 Esac 37 [ # Output: 38 is . 1 39 Not . 1 2 . 3 40 41 is # / bin /! the bash 42 is 43 is NUM = . 1 44 is Case $ NUM in 45 . 1 ) 46 is echo " . 1 " 47 ; & # unconditionally rearwardly match 48 2 ) 49 echo " 2 " 50 ;; 51 is . 3 ) 52 is echo "3" 53 ;; 54 * ) 55 echo "not 1 2 3" 56 ;; 57 esac 58 #输出:1