有时需要再脚本中检查多种条件。对此,可以使用嵌套的if-then语句。
要检查/etc/passwd文件中是否存在某个用户名以及该用户的主目录是否尚在,可以使用嵌套的if-then语句。嵌套部分位于主if-then-else语句的else代码块中:
$ cat test5.sh
#!/bin/bash
# testing nested ifs
#
testuser=NoSuchUser
#
if grep $testuser /etc/passwd
then
echo "The user $testuser account exists on this system."
echo
else
echo "The user $testuser does not exist on this system."
if ls -d /home/$testuser/
then
echo "However, $testuser has a directory."
fi
fi
echo "We are outside the nested if statements."
$ ls -d /home/NoSuchUser/
/home/NoSuchUser/
$
$ ./test5.sh
The user NoSuchUser does not exist on this system.
/home/NoSuchUser/
However, NoSuchUser has a directory.
We are outside the nested if statements.
这个脚本准确无误地发现,虽然用户账号已经从/etc/passwd文件中删除,但该用户的目录仍然存在。在脚本中使用这种嵌套if-then语句的问题在于代码不易阅读,逻辑流程很难厘清。
注意 用到的ls命令的一些使用选项(以及选项组合)。
-d:只显示目录,但不显示目录内容。
-sh:以人类易读的格式显示文件大小。
-g:在文件长列表中不显示文件属主。
-o:在文件长列表中不显示文件属组。
你可以使用else部分的另一种名为elif的形式,这样就不用再写多个if-then语句了。elif使用另一个if-then语句延续else部分:
if command1
then
commands
elif command2
then
more commands
fi
elif语句行提供了另一个要测试的命令,这类似于原始的if语句行。如果elif之后的命令的退出状态码是0,则bash会执行第二个then语句部分的命令。这种嵌套形式使得代码更清晰,逻辑更易懂:
$ cat test5.sh
#!/bin/bash
# testing nested ifs - using elif
#
testuser=NoSuchUser
#
if grep $testuser /etc/passwd
then
echo "The user $testuser account exists on this system."
echo
elif ls -d /home/$testuser/
then
echo "The user $testuser has a directory,"
echo "even though $testuser doesn't have an account."
fi
echo "We are outside the nested if statements."
$
$ ./test5.sh
/home/NoSuchUser/
The user NoSuchUser has a directory,
even though NoSuchUser doesn't have an account.
We are outside the nested if statements.
这个脚本还有一个问题:如果指定账户及其主目录都已经不存在了,那么脚本不会发出任何提醒。你可以解决这个问题,甚至可以更进一步,让脚本检查拥有主目录的不存在用户和没有主目录的不存在用户。这可以通过在嵌套elif中加入一个else语句来实现:
$ cat test5.sh
#!/bin/bash
# testing nested ifs - using elif and else
#
testuser=NoSuchUser
#
if grep $testuser /etc/passwd
then
echo "The user $testuser account exists on this system."
echo
elif ls -d /home/passwd
then
echo "The user $testuser has a directory,"
echo "even though $testuser doesn't have an account."
else
echo "The user $testuser does not exist on this system,"
echo "and no directory exists for the $testuser."
fi
echo "We are outside the nested if statements."
$
$ ./test5.sh
/home/NoSuchUser/
The user NoSuchUser has a directory,
even though NoSuchUser doesn't have an account.
We are outside the nested if statements.
$
$ sudo rmdir /home/NoSuchUser/
[sudo] password for christine:
$
$ ./test5.sh
ls: cannot access '/home/NoSuchUser/': No such file or directory
The user NoSuchUser does not exist on this system,
and no directory exists for the NoSuchUser.
We are outside the nested if statements.
在/home/NoSuchUser目录被删除之前,这个测试脚本执行的是elif语句,返回0值的退出状态。因此,elif的then代码块中的语句得以执行。删除了/home/NoSuchUser目录之后,elif语句返回的是非0值的退出状态。这使得elif块中的else代码块得以执行。
注意 记住,在elif语句中,紧跟其后的else语句属于elif代码块,不属于之前的if-then语句的代码块。
可以继续将多个elif语句串起来,形成一个更大的if-then-elif嵌套组合:
if command1
then
command set 1
elif command2
then
command set 2
elif command3
then
command set 3
elif command4
then
command set 4
fi
每个代码块会根据命令是否会返回退出状态码0来执行。记住,bash shell会依次执行if语句,只有第一个返回退出状态码0的语句中的then部分会被执行。
尽管使用了elif语句的代码看起来更清晰,但是脚本的逻辑仍然会让人犯晕。后续会介绍如何使用case命令代替大量的if-then语句嵌套。