一.使用 if-then 语句
1.最基本的结构化命令是 if-then 语句。if-then 语句的格式如下:
if command
then
commands
fi
或
if command; then
commands
fi
如果之前用过其他编程语言的 if-then 语句,那么这种形式可能会让你有点儿困惑。
在其他编程语言中,if 语句之后的对象是一个等式,其求值结果为 TRUE 或 FALSE。但 bash shell 的if 语句并非如此。
bash shell 的 if 语句会运行 if 之后的命令。如果该命令的退出状态码为0,那么位于 then 部分的命令就会被执行。
如果该命令的退出状态码是其他值,则 then 部分的命令不会被执行,bash shell 会接着处理脚本中的下一条命令。
fi 语句用来表示在if-then 语句到此结束。
例1
$ cat test1.sh
#!/bin/bash
# testing the if statement
if pwd
then
echo "it worked"
fi
$
在命令行中运行该脚本时,会得到如下结果:
$ ./test1.sh
/home/christine/scripts
It worked
$
例2
$ cat test2.sh
#!/bin/bash
# testing an incorrect command
if IamNotaCommand
then
echo "It worked"
fi
echo "We are outside the if statement"
$
$ ./test2.sh
./test2.sh: line 3: IamNotaCommand: command not found
We are outside the if statement
$
如果 if 语句行命令的退出状态值为 0,那么代码块中所有的命令都会被执行;否则,会跳过整个代码块:
$ cat test3.sh
#!/bin/bash
# testing multiple commands in the then block
#
testuser=christine
#
if grep $testuser /etc/passwd
then
echo "This is my first command in the then block."
echo "This is my second command in the then block."
echo "I can even put in other commands besides echo:"
ls /home/$testuser/*.sh
fi
echo "We are outside the if statement"
$
2.if-then-else 语句
if-then-else 语句在语句中提供了另外一组命令:
if command
then
commands
else
commands
fi
当 if 语句中的命令返回退出状态码 0 时,then 部分中的命令会被执行,这跟普通的 if-then语句一样。
当 if 语句中的命令返回非 0 退出状态码时,bash shell 会执行 else 部分中的命令。
$
$ cat test4.sh
#!/bin/bash
# testing the else section
#
testuser=NoSuchUser
#
if grep $testuser /etc/passwd
then
echo "The script files in the home directory of $testuser are:"
ls /home/$testuser/*.sh
echo
else
echo "The user $testuser does not exist on this system."
echo
fi
echo "We are outside the if statement"
$
$ ./test4.sh
The user NoSuchUser does not exist on this system.
We are outside the if statement
$
3.嵌套 if 语句
嵌套部分位于主 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."
可以使用 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."
$
这个脚本还有一个问题:如果指定账户及其主目录都已经不存在了,那么脚本不会发出任何提醒。
你可以解决这个问题,甚至可以更进一步,让脚本检查拥有主目录的不存在用户和没有主目录的不存在用户。
这可以通过在嵌套 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/$testuser/
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."
$
可以继续将多个 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