Case Base Syntax:
Format case?? Variable name in
value1)
Command
;;
value2)
Command
;;
*)
Commond
;;
Esac
In a case program, you can use the |, meaning, or means of a condition, such as???
2|3)
Command
;;
Script
[[email protected] shell]# vim case.sh #!/bin/bashread -p "Please input a number: " nif [ -z "$n" ]then echo "Please input a number." exit 1fin1=`echo $n|sed ‘s/[0-9]//g‘`if [ -n "$n1" ]then echo "Please input a number." exit 1fiif [ $n -lt 60 ] && [ $n -ge 0 ]then tag=1elif [ $n -ge 60 ] && [ $n -lt 80 ]then tag=2elif [ $n -ge 80 ] && [ $n -lt 90 ]then tag=3elif [ $n -ge 90 ] && [ $n -le 100 ]then tag=4else tag=0ficase $tag in 1) echo "不及格" ;; 2) echo "及格" ;; 3) echo "优秀" ;; 4) echo "非常优秀" ;; *) echo "The number range is 0-100." ;;esac
Execution results
[[email protected] shell]# sh case.shPlease input a number: 50不及格[[email protected] shell]# sh case.shPlease input a number: 60及格[[email protected] shell]# sh case.shPlease input a number: 80优秀[[email protected] shell]# sh case.shPlease input a number: 90非常优秀[[email protected] shell]# sh case.shPlease input a number: 116The number range is 0-100.[[email protected] shell]# sh case.shPlease input a number: aaaPlease input a number.
Shell Programming--case judgment