1. 程式人生 > >Linux Shell腳本編程case條件語句

Linux Shell腳本編程case條件語句

score 命令行參數 參數 $0 core 函數 調用 腳本編程 clas

1,判斷一個數字是否則在1,2,3之中.

#!/bin/bash
read -p "pls input a number:" n
case "$n" in
    1)
        echo "變量是1"
        ;;
    2)
        echo "變量是2"
        ;;
    3)
        echo "變量是3"
        ;;
    *)
        echo "pls input a number between 1 and 3"
        exit;
esac

2,多級if語句改寫

#!/bin/bash
read 
-p "pls input a number:" n if [ $n -eq 1 ]; then echo "$n是變量1" elif [ $n -eq 2 ]; then echo "$n是變量2" elif [ $n -eq 3 ]; then echo "$n是變量3" else echo "pls input a number between 1 and 3" fi

3,if..else嵌套,實現

#!/bin/bash
read -p "pls input a number:" n
if [ $n -eq 1 ]; then
echo 1 else if [ $n -eq 2 ]; then echo 2 elif [ $n -eq 3 ]; then echo 3 else echo "pls input a number [1-3]" fi fi

4,判斷 分數等級

#!/bin/bash

read -p "pls input score to test level:" score

if [ $score -ge 90 ]; then
    echo "優秀"
elif [ $score -ge 80
]; then echo "良好" elif [ $score -ge 70 ]; then echo "中等" elif [ $score -ge 60 ]; then echo "及格" else echo "不及格" fi

5,給文字加顏色

#!/bin/bash
RED_COLOR=\e[1;31m
GREEN_COLOR=\e[1;32m
YELLOW_COLOR=\e[1;33m
BLUE_COLOR=\e[1;34m
RESET_COLOR=\e[0m

echo     1, 悟空
    2, 八戒
    3, 唐僧
    4, 白龍馬
read -p "pls input a number:" n

case $n in
    1)
        echo -e  "${RED_COLOR}悟空${RESET_COLOR}"
        ;;
    2)
        echo -e  "${GREEN_COLOR}八戒${RESET_COLOR}"
        ;;
    3)
        echo -e  "${YELLOW_COLOR}唐僧${RESET_COLOR}"
        ;;
    4)
        echo -e  "${BLUE_COLOR}白龍馬${RESET_COLOR}"
        ;;
    *)
        echo "you need input a number in {1|2|3|4}"
esac

另一種寫法:

#!/bin/bash
RED_COLOR=\e[1;31m
GREEN_COLOR=\e[1;32m
YELLOW_COLOR=\e[1;33m
BLUE_COLOR=\e[1;34m
RESET_COLOR=\e[0m

function menu(){
    cat <<END
    1, 悟空
    2, 八戒
    3, 唐僧
    4, 白龍馬
END
}

function select_type(){
        read -p "pls input a number:" n
        case $n in
            1)
                echo -e  "${RED_COLOR}悟空${RESET_COLOR}"
                ;;
            2)
                echo -e  "${GREEN_COLOR}八戒${RESET_COLOR}"
                ;;
            3)
                echo -e  "${YELLOW_COLOR}唐僧${RESET_COLOR}"
                ;;
            4)
                echo -e  "${BLUE_COLOR}白龍馬${RESET_COLOR}"
                ;;
            *)
                echo "you need input a number in {1|2|3|4}"
        esac
}

function main(){
    menu
    select_type
}

main

讀取命令行參數,給內容設置顏色

#!/bin/bash
RED_COLOR=\e[1;31m
GREEN_COLOR=\e[1;32m
YELLOW_COLOR=\e[1;33m
BLUE_COLOR=\e[1;34m
RESET_COLOR=\e[0m

if [ $# -ne 2 ]; then
    echo "Usage $0 input {red|green|yellow|blue}"
    exit
fi

case $2 in
    red)
        echo -e "${RED_COLOR}$1${RESET_COLOR}"
        ;;
    green)
        echo -e "${GREEN_COLOR}$1${RESET_COLOR}"
        ;;
    yellow)
        echo -e "${YELLOW_COLOR}$1${RESET_COLOR}"
        ;;
    blue)
        echo -e "${BLUE_COLOR}$1${RESET_COLOR}"
        ;;
    *)    
        echo "usage $0 input {red|green|yellow|blue}"
        exit
esac

修改成函數調用方式

#!/bin/bash
function toColor(){
        RED_COLOR=\e[1;31m
        GREEN_COLOR=\e[1;32m
        YELLOW_COLOR=\e[1;33m
        BLUE_COLOR=\e[1;34m
        RESET_COLOR=\e[0m

        if [ $# -ne 2 ]; then
            echo "Usage $0 input {red|green|yellow|blue}"
            exit
        fi

        case $2 in
            red)
                echo -e "${RED_COLOR}$1${RESET_COLOR}"
                ;;
            green)
                echo -e "${GREEN_COLOR}$1${RESET_COLOR}"
                ;;
            yellow)
                echo -e "${YELLOW_COLOR}$1${RESET_COLOR}"
                ;;
            blue)
                echo -e "${BLUE_COLOR}$1${RESET_COLOR}"
                ;;
            *)    
                echo "usage $0 input {red|green|yellow|blue}"
                exit
        esac
}

function main(){
    toColor $1 $2
}

main $*

Linux Shell腳本編程case條件語句