12.3 使用结构化命令之三(嵌套if)

嵌套if

例1:

[root@CHENDAJIE neted-ifs]# cat test1 
#!/bin/bash
# Testing nested ifs
#
testuser=NoSuchUser
#
if grep $testuser /etc/passwd
then
        echo "The user $testuser exits on this system."
else
        echo "The user $testuser does not exits on this system."
        if grep chendajie /etc/passwd
        then
                echo "But,we have chendajieon this system."
        fi
fi
[root@CHENDAJIE neted-ifs]# chmod u+x test1 
[root@CHENDAJIE neted-ifs]# ./test1 
The user NoSuchUser does not exits on this system.
chendajie:x:1000:1000:chendajie:/home/chendajie:/bin/bash
But,we have chendajieon this system.

这个脚本先检查了etc/passwd中某个用户名是否存在,在发现该用户名不存才的时候,继续检查了chendajie用户是否存在。

elif

elif使用另一个if-then语句延续else部分
格式:

if command1
then
	commands
elif command2
then
	more commands
fi

elif语句行提供了另一个要测试的命令,这类似于原始的if语句行。如果elif后命令的退出状态码是0,则bash会执行第二个then语句部分的命令。使用这种嵌套方法,代码更清晰,逻辑更易懂。
例2:
因为我不想在系统中创建一个NoSuchUser用户之后再去删除这个用户(指的是不删除家目录的那种)所以我提前在的我/home目录下创建了一个名字是NoSuchUser目录。

[root@CHENDAJIE chendajie]# cd /home
[root@CHENDAJIE home]# ls
chendajie
[root@CHENDAJIE home]# mkdir NoSuchUser
[root@CHENDAJIE home]# ls
chendajie  NoSuchUser

下面我们开始测试elif语句:

[root@CHENDAJIE neted-ifs]# cat elif.test 
#!/bin/bash
# Testing nested ifs - us elif
#
testuser=NoSuchUser
#
if grep $testuser /etc/passwd
then
        echo "The user $testuser exists on this system."
#
elif ls -d /home/$testuser
then
        echo "The user $testuser does not exits on this system."
        echo "However,we got a directory is $testuser."
fi
[root@CHENDAJIE neted-ifs]# chmod u+x elif.test 
[root@CHENDAJIE neted-ifs]# ./elif.test 
/home/NoSuchUser
The user NoSuchUser does not exits on this system.
However,we got a directory is NoSuchUser.

在这个脚本中,先去/etc/passwd文件中检查了NoSuchUser是否存在。在判断出不存在这个用户时,去/home目录下检查了是否存在NoSchUser文件。并将信息打印出来。
例3:
在例2的基础上更进一步,让脚本检查拥有目录的不存在用户以及没有拥有目录的不存在用户。这可以通过在嵌套elif中加入一个else语句来实现。
首先我将/home下的NoSuchUser目录删掉。



[root@CHENDAJIE home]# rm -rf NoSuchUser/
[root@CHENDAJIE home]# ls
chendajie
[root@CHENDAJIE neted-ifs]# cat elif.test2
#!/bin/bash
# Testing nested ifs - use elif & else
#
testuser=NoSuchUser
#
if grep $testuser /etc/passwd
then
        echo "The user $testuser exists on this system."
#
elif ls -d /home/$testuser
then
        echo "The user $testuser does not exist on this system."
        echo "However, wo got a $testuser directory."
#
else
        echo "The user $testuser does not exist on this system."
        echo "And,$testuser does not have a diretory."
fi
[root@CHENDAJIE neted-ifs]# chmod u+x elif.test2
[root@CHENDAJIE neted-ifs]# ./elif.test2
ls: 无法访问/home/NoSuchUser: 没有那个文件或目录
The user NoSuchUser does not exist on this system.
And,NoSuchUser does not have a diretory.

在这个脚本中,先使用elif语句去检查系统中是否有NoSuchUser这个用户或者有该名字命名的目录,然后再嵌套了一个else检查了系统中不存在以NoSchUser名字的目录或用户。
下面我在/home目录中添加一个NoSuchUser目录重新执行一下elif.test2

[root@CHENDAJIE home]# mkdir NoSuchUser
[root@CHENDAJIE home]# ls
chendajie  NoSuchUser
[root@CHENDAJIE neted-ifs]# ./elif.test2
/home/NoSuchUser
The user NoSuchUser does not exist on this system.
However, wo got a NoSuchUser directory

窍门:在elif语句中,紧跟其后的else语句属于elif代码块。它们并不属于之前的if–then代码块。
可以寄继续将多个elif语句串起来,形成一个大的if–then–elif嵌套组合。

if command1
then
	command set 1
elif command 2
then
	command set2
elif command 3
then
	command set3
elif command 4
then
	command set 4
fi

.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值