Shell入门-case条件语句

Shell中case条件语句

本篇主要包含:

  • case语句的格式及写法

语法格式:

case "arg1" in
 值1)
 	指令
 	;;
 值2)
 	指令
 	;;
 *)
 	指令
esac

实践1

根据用户输入的数字做出响应,很类似Java中的switch case语法

[root@localhost shell]# cat 9_1.sh 
#!/bin/bash
#***********************************************
#Author:        luotianhao
#Mail:          [email protected]
#Version:       1.0
#Date:          2021-03-11
#FileName:      9_1.sh
#Description:   This is a test script.
#***********************************************

read -p "please input a number:" ans

case "$ans" 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 $ans"
        ;;
    *)
        echo "please input [0-9] int"
        exit;
esac

实践2

打印特殊颜色:

  • echo -e可以识别转义字符,这里将识别特殊字符的含义,并输出。
  • \E可以使用\033替代。
  • “[1”数字1表示加粗显示(可以加不同的数字,以代表不同的意思,详细信息可用man console_codes获得)
  • 31m表示为红色字体,可以换成不同的数字,以代表不同的意思
  • “红色字oldboy”表示待设置的内容。
  • “[0m”表示关闭所有属性,可以换成不同的数字,以代表不同的意思。

有关ANSI控制码的说明如下。

  • \33[0m 表示关闭所有属性。
  • \33[1m 表示设置高亮度。
  • \33[4m 表示下划线。
  • \33[5m 表示闪烁。
  • \33[7m 表示反显。
  • \33[8m 表示消隐。
  • \33[30m – \33[37m 表示设置前景色。
  • \33[40m – \33[47m 表示设置背景色
[root@localhost shell]# echo -e "\E[1;31m红色字oldboy\E[0m"
红色字oldboy
[root@localhost shell]# echo -e "\033[31m红色字oldboy\033[0m"
红色字oldboy

加颜色的示例:

[root@localhost shell]# cat plush_color.sh 
#!/bin/bash
#***********************************************
#Author:        luotianhao
#Mail:          [email protected]
#Version:       1.0
#Date:          2021-03-11
#FileName:      plush_color.sh
#Description:   This is a test script.
#***********************************************

RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
#关闭所有属性
RES='\E[0m'

echo -e "$RED_COLOR oldboy $RES"

echo -e "$YELLOW_COLOR oldgirl $RES"

当用户输入对应的数字选择水果的时候,告诉他选择的水果是什么,并给水果单词加上一种颜色(随意),要求用case语句来实现。

[root@localhost shell]# cat 9_2.sh 
#!/bin/bash
#***********************************************
#Author:        luotianhao
#Mail:          [email protected]
#Version:       1.0
#Date:          2021-03-11
#FileName:      9_2.sh
#Description:   This is a test script.
#***********************************************
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'

RES='\E[0m'

echo '
  ==================================
  1.apple
  2.pear
  3.banana
  4.cherry
  ==================================
'

read -p "pls select a num:" num

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

使用cat来打印菜单

[root@localhost shell]# cat 9_3.sh 
#!/bin/bash
#***********************************************
#Author:        luotianhao
#Mail:          [email protected]
#Version:       1.0
#Date:          2021-03-11
#FileName:      9_2.sh
#Description:   This is a test script.
#***********************************************
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'

RES='\E[0m'

menu() {
    
    
cat <<END
  ==================================
  1.apple
  2.pear
  3.banana
  4.cherry
  ==================================
END
}
menu
read -p "pls select a num:" num

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

将方法封装

[root@localhost shell]# cat 9_4.sh 
#!/bin/bash
#***********************************************
#Author:        luotianhao
#Mail:          [email protected]
#Version:       1.0
#Date:          2021-03-11
#FileName:      9_2.sh
#Description:   This is a test script.
#***********************************************
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: $0 {1|2|3|4}"
  exit 1
}

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

function chose(){
    
    

read -p "pls select a num:" num

case "$num" in 
    1)
        echo -e "${RED_COLOR}apple${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
  chose
}
main

猜你喜欢

转载自blog.csdn.net/weixin_43169156/article/details/114794115