Shell---判断(if)和分支(case)

简介: 版权声明:本文为博主原创文章,转载请注明出处。 https://blog.
版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/twilight_karl/article/details/78373107

if 判断

单分支if条件语句

语法:

if [ 条件判断式 ];then
    # do something
fi

或者

if [ 条件判断式 ]
    then
    # do something
fi

案例一,获取当前用户名,当然用 whoami 也可:

#!/bin/bash

test=`env | grep "USER" | cut -d "=" -f 2`
if [ test=="root" ];then
        echo "This user is root"
fi

案例二,检查磁盘容量:

#!/bin/bash

rate=`df -h | grep "sda1" | awk '{ print $5 }' | cut -d "%" -f 1`

if [ $rate -gt "10" ];then
                echo "/ is Full"
fi

PS:
- 条件判断式就是test命令判断,可以用test命令替代,中括号中间必须有空格
- -gt 当变量值为空时会报错

双分支if

语法:

if [ 条件判断式 ]
    then 
        ...
    else 
        ...
fi 

案例三,判断目录的属性:

#!/bin/bash

read -t 30 -p "input a dir:" dir

if [ -d $dir ]
        then
                echo "This is a directory"
        else
                echo "no no no "
fi

案例四,判断tomcat服务是否开启:

#!/bin/bash

test=`ps aux | grep tomcat | grep -v grep`

if [ -n "$test" ]
        then
                echo "tomcat is running"
        else
                echo "tomcat is not running"
               /etc/init.d/tomcat start
#                service tomcat start  不建议使用
                echo "tomcat is started"
fi

PS:
- if判断中的变量最好加上引号,因为如果变量的值中包含空格,将出现too many arguments 错误,所以还是写成 if [ -n "$test" ] 较安全

多分支if

语法:

if [ 条件判断 ]
    then 
        # do something
elif [ 条件判断 ]
    then 
        # do something

...

else 
    # do something
fi

案例五,简易计算器:

#!/bin/bash

read -p "please input num1:" num1
read -p "please input operator:" op
read -p "please input num2:" num2

if [ -z "$num1" -o -z "$num2" -o -z "$op" ]
        then
                echo "value shoud not be null"
                exit 2
        else
                # 把所有数字替换成空,如果替换后不为空,则表示变量中不符合规范 
                test1=`echo $num1 | sed 's/[0-9]//g'`
                test2=`echo $num2 | sed 's/[0-9]//g'`

                if [ -n "$test1" -o -n "$test2" ];then
                        echo "数值格式错误"
                        exit 4
                fi

                if [ "$op" == "+" ]
                        then
                                result=$(($num1+$num2))
                elif [ "$op" == "-" ]
                        then
                                result=$(($num1-$num2))
                elif [ "$op" == "*" ]
                        then
                                result=$(($num1*$num2))
                elif [ "$op" == "/" ]
                        then
                                let "result=$num1/$num2"
                else
                        echo "operator is wrong"
                        exit 3
                fi

fi

echo ${num1}${op}${num2}=${result}

案例六,判断文件类型:

#!/bin/bash

read -p "请输入文件或目录名:" path

if [ -z "$path" ]
        then
                echo "请输入内容!"
                exit 10
elif [ ! -e "$path" ]
        then
                echo "文件或目录不存在"
                exit 11
elif [ -f "$path" ]
        then
                echo "输入的是一个文件"
elif [ -d "$path" ]
        then
                echo "输入的是一个目录"
else
        echo "输入的式其他类型的文件"
fi

case 分支

语法:

case $变量名 in
    "值1")
        // do something,变量的值等于值1
        ;;
    "值2")
        // do something,变量的值等于值2
        ;;

    ...

    *)
        // do something,变量的值与上面都不同
    ;;
esca

案例

#!/bin/bash

read -p "请输入 yes/no" result

case "$result" in
        "yes")
                echo "你输入的式yes"
                ;;
        "no")
                echo "你输入的是no"
                ;;
        *)
                echo "请输入正确的选择"
esac
相关文章
|
15天前
|
Shell
shell学习(六) 【case多条件分支语句】
shell学习(六) 【case多条件分支语句】
11 1
|
2月前
|
Shell
Shell脚本中的`case`语句
Shell脚本中的`case`语句
34 5
|
4月前
|
Shell Linux iOS开发
Shell的`case`语句
Shell的`case`语句
26 2
|
6月前
|
Shell Linux Go
《Linux操作系统编程》第八章 Shell程序设计: shell 语言结构,包括测试、分支、循环、跳转、函数、语句组
《Linux操作系统编程》第八章 Shell程序设计: shell 语言结构,包括测试、分支、循环、跳转、函数、语句组
67 0
|
7月前
|
Shell
shell编程之条件语句与case语句
shell编程之条件语句与case语句
39 2
|
8月前
|
应用服务中间件 网络安全 nginx
shell&case语句
流程控制语句-case用来实现程序流程的选择、循环等进行控制,类似于if,但更具准确性一般用于服务的启动、停止脚本
|
8月前
|
运维 Shell 应用服务中间件
【运维知识高级篇】超详细的Shell编程讲解3(if判断+Shell菜单+case流程判断+批量创建删除用户+猜数字小游戏)
【运维知识高级篇】超详细的Shell编程讲解3(if判断+Shell菜单+case流程判断+批量创建删除用户+猜数字小游戏)
114 1
|
8月前
|
Shell
Shell case 语法简单案例
Shell case 语法简单案例
43 0
|
9月前
|
NoSQL Shell Redis
RHCE的一道shell脚本编程题&理解shell case 语法
RHCE的一道shell脚本编程题&理解shell case 语法
53 0
|
9月前
|
Shell
Shell脚本:case语句
Shell脚本:case语句
76 1