|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 2022-03-26 23:08  yanqi_vip  阅读(30)  评论(0)    收藏  举报

导航