shell脚本中的逻辑判断 文件目录属性判断 if特殊用法 case判断
case判断
- • 格式 case 变量名 in
- value1)
- command
- ;;
- value2)
- command
- ;;
- *)
- commond
- ;;
- esac
- • 在case程序中,可以在条件中使用|,表示或的意思, 比如
- |)
- command
- ;;
- shell脚本案例
- #!/bin/bash
- read -p "Please input a number: " n
- if [ -z "$n" ]
- then
- echo "Please input a number."
- exit
- fi
- n1=`echo $n|sed 's/[0-9]//g'`
- if [ -n "$n1" ]
- then
- echo "Please input a number."
- exit
- fi
- if [ $n -lt ] && [ $n -ge ]
- then
- tag=
- elif [ $n -ge ] && [ $n -lt ]
- then
- tag=
- elif [ $n -ge ] && [ $n -lt ]
- then
- tag=
- elif [ $n -ge ] && [ $n -le ]
- then
- tag=
- else
- tag=
- fi
- case $tag in
- )
- echo "not ok"
- ;;
- )
- echo "ok"
- ;;
- )
- echo "ook"
- ;;
- )
- echo "oook"
- ;;
- *)
- echo "The number range is 0-100."
- ;;
- esac
- 讲解
- 输入一个数字 read -p "please input a number" n
- -ge (greater equeal大于等于)
- -le (less than equal小于等于)