选择执行if语句
格式:
if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
单分支
if 判断条件; then 条件为真的分支代码 ...... fi
双分支
if 判断条件; then 条件为真的分支代码1 ...... else 条件为假的分支代码2 ...... fi
多分支
if 判断条件1; then 条件1为真的分支代码1 …… elif 判断条件2; then 条件2为真的分支代码2 …… elif 判断条件3; then 条件3为真的分支代码3 …… ... else 以上条件都为假的分支代码N …… fi
注:
- 多个条件时,逐个条件进行判断,第一次为“真”条件时,执行其分支,而后结束整个if语句
- if语句可嵌套
例1:
[root@CentOS7 ~]# cat yesorno_sh.sh #!/bin/bash # #************************************************************************************* #Author: wanghaomiao #QQ: 1074035975 #Date: 2022-01-08 #FileName yesorno.sh #URL: www.wanghaomiao.com.cn #Description: The test script #Copyreght (C): 2022 All rightsreserved #************************************************************************************* read -p "Are you OK (yes/no)? " answer if [[ $answer =~ ^[Yy]$|^[Yy][Ee][Ss]$ ]]; then echo "yes" elif [[ $answer =~ ^[Nn]$|^[Nn][Oo]$ ]]; then echo "no" else echo "Input false,please input yes or no!" fi
例2:
[root@CentOS7 ~]# cat if_bmi.sh #!/bin/bash # #************************************************************************************* #Author: wanghaomiao #QQ: 1074035975 #Date: 2022-01-09 #FileName if_bmi.sh #URL: www.wanghaomiao.com.cn #Description: The test script #Copyreght (C): 2022 All rightsreserved #************************************************************************************* COLOR="\E[$[RANDOM%7+31];1m" COLOR_END="\E[0m" echo -ne "${COLOR}" read -p "Please enter your height(cm): " HIGH echo -ne "${COLOR_END}" if [[ ! "$HIGH" =~ ^[0-2]([0-9]{,2}?)$ ]];then echo -e ""$COLOR"Incorrectheight,please re-enter!$COLOR_END" exit 1 fi echo -ne "${COLOR}" read -p "Please enter your weight(KG): " WEIGHT echo -ne "${color}" if [[ ! "$WEIGHT" =~ ^[0-9]{1,3}\.[0-9]$ ]];then echo -e ""$COLOR"Incorrect weight,please re-enter!$COLOR_END" exit 1 fi BMI=`echo $WEIGHT/$HIGH^2|bc` if [ $BMI -le 18 ];then echo -e ""$COLOR"You're too skinny!$COLOR_END" elif [ $BMI -lt 24 ];then echo -e ""$COLOR"good figure!$COLOR_END" else echo -e ""$COLOR"You're too fat!$COLOR_END" fi
条件判断case语句
格式:
case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac
case 变量引用 in PAT1) 分支1 ;; PAT2) 分支2 ;; PAT3) 分支3 ;; ... *) 默认分支 ;; csac
case支持glob风格的通配符:
* 任意长度的任意字符 ? 任意单个字符 [] 指定范围内的任意单个字符 | 或者,如:a|b
例1:
[root@CentOS7 ~]# cat yesorno_case.sh #!/bin/bash # #************************************************************************************* #Author: wanghaomiao #QQ: 1074035975 #Date: 2022-01-08 #FileName yesorno.sh #URL: www.wanghaomiao.com.cn #Description: The test script #Copyreght (C): 2022 All rightsreserved #************************************************************************************* read -p "Are you OK (yes/no)? " answer case $answer in [Yy]|[Yy][Ee][Ss]) echo "yes" ;; [Nn]|[Nn][Oo]) echo "no" ;; *) echo "Input false,please input yes or no!" ;; esac
[root@CentOS7 ~]# cat yesorno_case1.sh #!/bin/bash # #************************************************************************************* #Author: wanghaomiao #QQ: 1074035975 #Date: 2022-01-08 #FileName yesorno.sh #URL: www.wanghaomiao.com.cn #Description: The test script #Copyreght (C): 2022 All rightsreserved #************************************************************************************* read -p "Are you OK (yes/no)? " answer answer=`echo $answer | tr 'A-Z' 'a-z'` case $answer in y|yes) echo "yes" ;; n|no) echo "no" ;; *) echo "Input false,please input yes or no!" ;; esac
例2:
[root@CentOS7 ~]# cat work_menu.sh #!/bin/bash # #************************************************************************************* #Author: wanghaomiao #QQ: 1074035975 #Date: 2022-01-09 #FileName work_menu.sh #URL: www.wanghaomiao.com.cn #Description: The test script #Copyreght (C): 2022 All rightsreserved #************************************************************************************* echo -ne "\E[$[RANDOM%7+31];1m" cat <<EOF 请选择: 1)备份数据库 2)清理日志 3)软件升级 4)软件回滚 5)删库跑路 EOF echo -ne '\E[0m' read -p "请输入上面数字1-5: " MENU case $MENU in 1) ./buckup.sh ;; 2) echo "清理日志" ;; 3) echo "软件升级" ;; 4) echo "软件回滚" ;; 5) echo "删库跑路" ;; *) echo "INPUT FALSE!" esac