嵌套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
.