|NO.Z.00022|——————————|LinuxShell|——|Linux&Shell&流程控制.V03|——|多分支if条件判断|

一、多分支if条件判断
### --- 多分支if条件判断语法

if [ 条件判断式 1 ]
    then
        当条件判断式 1 成立时,执行程序 1
elif [ 条件判断式 2 ]
    then
        当条件判断式 2 成立时,执行程序 2
…省略更多条件…
else
        当所有条件都不成立时,最后执行此程序
fi
### --- 那我们再写一个例子,用 if 多分支条件语句来判断一下用户输入的是一个文件,还是一个目录:
~~~		判断用户输入的是什么文件

[root@localhost ~]# vi sh/if-elif.sh
#!/bin/bash
#判断用户输入的是什么文件
# Author: shenchao (E-mail: shenchao@atguigu.com)
read -p "Please input a filename: " file
#接收键盘的输入,并赋予变量 file


if [ -z "$file" ]
#判断 file 变量是否为空
    then
        echo "Error,please input a filename"
        #如果为空,执行程序 1,也就是输出报错信息
        exit 1
        #退出程序,并返回值为 1(把返回值赋予变量$?)
elif [ ! -e "$file" ]
#判断 file 的值是否存在
    then
        echo "Your input is not a file!"
        #如果不存在,则执行程序 2
        exit 2
        #退出程序,把并定义返回值为 2
elif [ -f "$file" ]
#判断 file 的值是否为普通文件
    then
        echo "$file is a regulare file!"
        #如果是普通文件,则执行程序 3
elif [ -d "$file" ]
#判断 file 的值是否为目录文件
    then
        echo "$file is a directory!"
        #如果是目录文件,则执行程序 4
else
        echo "$file is an other file!"
        #如果以上判断都不是,则执行程序 5
fi

 






 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(20)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示