zoukankan      html  css  js  c++  java
  • case语句

    case条件语句的语法格式:

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

    实例:

    #!/bin/bash
    
    RED_COLOR="E[1;31m"
    GREEN_COLOR="E[1;32m"
    YELLOW_COLOR="E[1;33m"
    BLUE_COLOR="E[1;34m"
    RES="E[0m"
    
    cat << END
            =============================
            1.apple
            2.pear
            3.banana
            4.cherry
            =============================
    END
    
    read -p "Please input a number to choice." 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 "Must be input {1|2|3|4}"
    esac
    ~      

    运行结果:

    [root@rhel6 script]# bash case02.py 
            =============================
            1.apple
            2.pear
            3.banana
            4.cherry
            =============================
    Please input a number to choice.1
    apple
    [root@rhel6 script]# bash case02.py 
            =============================
            1.apple
            2.pear
            3.banana
            4.cherry
            =============================
    Please input a number to choice.2
    pear
    [root@rhel6 script]# bash case02.py 
            =============================
            1.apple
            2.pear
            3.banana
            4.cherry
            =============================
    Please input a number to choice.3
    banana
    [root@rhel6 script]# bash case02.py 
            =============================
            1.apple
            2.pear
            3.banana
            4.cherry
            =============================
    Please input a number to choice.4
    cherry
  • 相关阅读:
    define和typedef
    keil5配置stm32库函数开发
    SPI、CAN、I2C
    flash,sram
    关于网络地址
    关于定时器、波特率、TH和TL值的计算
    关于串口工作方式
    ad各层
    AD快捷键
    OAuth2.0 微博登陆网站功能的实现(一)获取用户授权及令牌 Access Token
  • 原文地址:https://www.cnblogs.com/vincenshen/p/6594403.html
Copyright © 2011-2022 走看看