Linux Shell脚本编程case条件语句

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 $*

原文地址:https://www.cnblogs.com/ghostwu/p/9114024.html

时间: 2024-07-31 16:26:14

Linux Shell脚本编程case条件语句的相关文章

Linux Shell脚本编程while语句

Linux Shell脚本编程while语句案例 1,每隔3秒,打印一次系统负载 #!/bin/bash while truedo    uptime    sleep 3done 2,把监控结果保存到文件,在后台执行,然后用tail -f监控文件变化[email protected]:~/linux/shell/flow_control$ sh while.sh &[1] 12867 #!/bin/bash while truedo    uptime >> log.txt    s

Linux Shell脚本编程学习笔记和实战

http://www.1987.name/141.html shell基础 终端打印.算术运算.常用变量 Linux下搜索指定目录下特定字符串并高亮显示匹配关键词 从键盘或文件中获取标准输入 [read命令] 文件的描述符和重定向 数组.关联数组和别名使用 函数的定义.执行.传参和递归函数 条件测试操作与流程控制语句 获取时间日期格式和延时 [date.sleep命令] 内部字段分隔符IFS和脚本的调试DEBUG 显示.读取或拼接文件内容 [cat命令] 文件查找与打印文件列表 [find命令]

Linux Shell脚本 几种循环语句创建用户的方法

大家好,我是孤云暮雨,今天给大家带来的是"Linux Shell脚本 几种循环语句创建用户的方法" 添加user1-user20用户 for循环: #!/bin/bash for i in {1..20} do useradd user$i echo "user$i Users to add success" done for循环(C风格): #!/bin/bash for ((i=1;i<=20;i++)) do useradd user$i &&a

shell脚本编程:条件判断if语句使用小结

shell脚本编程,有三种控制结构分别是:顺序结构,条件判断结构,循环结构.本文将总结shell脚本中条件判断结构的使用方法. 条件判断结构分为三种,单分支,双分支,多分支,等结构. 单分支结构的语法如下: if [ expression  ] ;then statement1 statement2 ......... fi 双分支语法结构: if [ expression ];then statement1 statement2 ..... else statement3 statement4

Linux shell脚本编程详解及应用实例

什么是shell脚本? 1.shell脚本:是一种解释型语言,不需要提前进行编译,只需将代码转化成中间代码,边解释边运行,执行效率稍逊于编译型语言,跨平台性好.而编译型语言则需要提前进行编译转化为二进制文件,靠近底层硬件执行效率高,可移植性差. 2.shell的首行严格来说使用shebang机制:由#和!构成的字符序列,在类unix系统中程序的载入器将其后的内容,当做解释器的指令,并将载有shebang文件路径作为解释器的参数,且予以调用. shell及其他解释型语言的一般格式?  #!/bin

Linux shell脚本编程基础之练习篇

shell脚本编程基础之练习篇. 1.编写一个脚本使我们在写一个脚本时自动生成”#!/bin/bash”这一行和注释信息. #!/bin/bash if [ $# -ne 1 ] then echo "请输入一个参数" exit else echo "参数正确" newfile=$1 fi #echo `grep "^#\!" ${newfile}` if ! grep "^#\!" ${newfile} &>/

Shell脚本编程——case语句

天我为大家分享的是关于如何用case语句去写一些选择执行的脚本.case语句主要合适应用于一些选择条件比较复杂的脚本中,如果我们用if多分支语句也能写出来,但是显的太哆嗦,此时我们的最佳选择就就是case语句. 条件判断:case语句      case 变量引用 in      PAT1)      分支1      ;;      PAT2)      分支2      ;;      ...      *)      默认分支      ;;      esac 示例代码: 1 #!/bi

Linux Shell脚本编程while语句案例

1,每隔3秒,打印一次系统负载 #!/bin/bash while true do uptime sleep 3 done 2,把监控结果保存到文件,在后台执行,然后用tail -f监控文件变化 [email protected]:~/linux/shell/flow_control$ sh while.sh & [1] 12867 #!/bin/bash while true do uptime >> log.txt sleep 3 done [email protected]:~/

Unix/Linux shell脚本编程学习--Shell Script II

Shell Script II 10.Shell echo命令 echo "OK!\n”   #显示换行 echo "It is a test" echo无拼接字符时后一般可以不使用”引号”,从上面可看出,双引号可有可无,单引号主要用在原样输出中. 显示结果重定向保存至文件: vim myfile 创建文件 echo "It is a test" > myfile cat myfile 查看文件内容 若需要原样输出字符串(不进行转义),请使用单引号.