结构化命令

一.使用if-then语句
1.最近本的结构化命令就是if-then语句。 的if语句会运行if后面的那个命令。如果该命令的退出状态码(参见第11章)是0(该命令成功运行),位于then部分的命令就会被执行。如果该命令的退出状态码是其他值,then部分的命令就不会被执行,bash shell会继续执行脚本中的下一个命令。fi语句用来表示if-then语句到此结束。

if command(命令)
then
commands(命令)
fi

例:

$ cat test1.sh 
#!/bin/bash 
# testing the if statement 
if pwd 
  then 	 
    echo "It worked" 
fi
$

这个脚本在if行采用了pwd命令。如果命令成功结束,echo语句就会显示该文本字符串。在命令行运行该脚本时,会得到如下结果。

$ ./test1.sh 	
/home/Christine 
It worked 
$

2.if-then-else语句,在if-then语句中,不管命令是否成功执行,你都只有一种选择。如果命令返回一个非零退出状态码,bash shell会继续执行脚本中的下一条命令。在这种情况下,如果能够执行另一组命令就好了。这正是if-then-else语句的作用。
if command
then
commands
else
commands
fi

例:

$ cat test4.sh
#!/bin/bash 
# testing the else section 
# 
testuser=NoSuchUser 
# 
if grep $testuser /etc/passwd 
  then 
    echo "The bash files for user $testuser are:" 
    ls -a /home/$testuser/.b* 
    echo 
else 
    echo "The user $testuser does not exist on this system." 
    echo 
fi 
$
$ ./test4.sh
The user NoSuchUser does not exist on this system. 
$ 

这样就更友好了。跟then部分一样,else部分可以包含多条命令。fi语句说明else部分结束了。

3.嵌套if
有时你需要检查脚本代码中的多种条件。对此,可以使用嵌套的if-then语句。要检查/etc/passwd文件中是否存在某个用户名以及该用户的目录是否尚在,可以使用嵌套的if-then语句。嵌套的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 exists on this system." 
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 
$ 
$ ./test5.sh
The user NoSuchUser does not exist on this system. 
/home/NoSuchUser/ 
However, NoSuchUser has a directory. 
$ 

这个脚本准确无误地发现,尽管登录名已经从/etc/passwd中删除了,但是该用户的目录仍然存在。在脚本中使用这种嵌套if-then语句的问题在于代码不易阅读,很难理清逻辑流程。可以使用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 - use 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 exist on this system." 
	  echo "However, $testuser has a directory." 
# 
fi 
$ 
$ ./test5.sh
/home/NoSuchUser 
The user NoSuchUser does not exist on this system. 
However, NoSuchUser has a directory. 
$

甚至可以更进一步,让脚本检查拥有目录的不存在用户以及没有拥有目录的不存在用户。这可以通过在嵌套elif中加入一个else语句来实现。

$ cat test5.sh
#!/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, $testuser has a directory." 
# 
  else 
     echo "The user $testuser does not exist on this system." 
     echo "And, $testuser does not have a directory." 
fi 
$ 
$ ./test5.sh
/home/NoSuchUser 
The user NoSuchUser does not exist on thi
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值