Shell脚本学习-case条件语句

case条件语句相当于多分支的if/elif/else条件语句,但是它更规范工整。常被应用于实现系统服务启动脚本等企业应用场景中。

语法结构:

case  "变量" in
  值1)
    指令1...
     ;;
  值2)
    指令2...
    ;;
  *)
    指令3...
esac

说明:

1)case语句获取的变量的值与表达式部分的值等逐一进行比较;如果匹配,就执行值后面对应的指令,直到遇到双分号为止。然后跳出case语句的主体,执行esac后面的语句。

2)如果没有匹配到变量的任何值,则执行*)后面的指令,这个地方通常是给使用者提示信息usage。这部分相当于if多分支结构最后的else语句部分。

3)书写的时候注意不同行内容的缩进距离。

4)case语句相当于一个if的多分支结构语句。

示例1:

根据用户的输入判断用户输入的是哪个数字?

[root@vm1 scripts]# cat judge_num.sh
#!/bin/bash
#

read -p "Please input a number: " i

case "$i" in
  1)
    echo "The num you input is 1"
    ;;
  2)
    echo "The num you input is 2"
    ;;
  [3-9])
    echo "The num you input is $i"
    ;;
  *)
    echo "Please input [0-9] int."
    exit
esac

测试结果:

[root@vm1 scripts]# sh judge_num.sh
Please input a number: 5
The num you input is 5
[root@vm1 scripts]# sh judge_num.sh
Please input a number: 2
The num you input is 2
[root@vm1 scripts]# sh judge_num.sh
Please input a number: 9
The num you input is 9
[root@vm1 scripts]# sh judge_num.sh
Please input a number: 10
Please input [0-9] int.

可以看到case比if语句更简洁更规范。

示例2:

根据用户的选择输入判断是哪种水果并加上不同的颜色。

[root@vm1 scripts]# cat fruit_menu_select.sh
#!/bin/bash
#

# color defined      #把颜色定义成变量,方便使用
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'

cat << EOF
==========
1.apple
2.pear
3.banana
4.cherry
==========
EOF

read -p "Please select a fruit: " num

case "$num" in
  1)
    echo -e "${RED_COLOR}apples${RES}"
    ;;
  2)
    echo -e "${GREEN_COLOR}pear${RES}"
    ;;
  3)
    echo -e "${YELLOW_COLOR}banana${RES}"
    ;;
  4)
    echo -e "${BLUE_COLOR}cherry${RES}"
    ;;
  *)
    echo "your input must be {1|2|3|4}"
esac

测试结果:

Shell脚本学习-case条件语句_第1张图片

代码说明:

1)echo -e可以识别转义字符;

2)\E 可以使用\033代替;

3)[1 数字1可以加粗显示显示。

4)31m表示红色的字体;

5)[0m 表示关闭属性,可以换成不同的数字,代表不同意思。

例如:

\E[0m:表示关闭所有属性;

\E[1m: 表示设置高亮度;

\E[4m: 表示下划线

\E[5m: 表示闪烁

\E[7m: 表示反显

\E[8m: 表示消隐

可以通过man_console_codes来查看。

在这个过程中,我们遇到一个问题,通过man console_codes 有个提示信息:

No manual entry for console_codes。

然后我这边执行如下命令:

yum install -y man-pages

Shell脚本学习-case条件语句_第2张图片

然后再执行man console_codes就可以查看了。

 Shell脚本学习-case条件语句_第3张图片

[root@vm1 scripts]# cat fruit_menu_select2.sh
#!/bin/bash
#

# color defined
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'

function usage() {
    echo "USAGE: Your choise must be {1|2|3|4}"
    exit 1
}

function menu() {
cat << END
    ==========
    1.apple
    2.pear
    3.banana
    4.cherry
    ==========
END
}

function choose() {
read -p "Please select a choise: " fruit

case "$fruit" in
  1)
    echo -e "${RED_COLOR}apples${RES}"
    ;;
  2)
    echo -e "${GREEN_COLOR}pear${RES}"
    ;;
  3)
    echo -e "${YELLOW_COLOR}banana${RES}"
    ;;
  4)
    echo -e "${BLUE_COLOR}cherry${RES}"
    ;;
  *)
    usage
esac
}

function main(){
    menu
    choose
}

main

 将这段代码进行了优化,使用函数模块化,看起来美观了不少。

可以将这块的思想渗透到工作的代码中,我们在工作中可以将正确的信息使用绿色标注出来,将错误的信息用红色标注出来,一般的警告使用蓝色标注出来。那么在脚本跑的时候就能很快判断脚本的是否有错误,这是我再工作中使用颜色标注的体会。

另外,我们推荐在脚本中定义变量的方式来给字体加颜色。

# color defined
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
PINK_COLOR='\E[1;35m'
RES='\E[0m'

你可能感兴趣的:(Shell,linux)