码迷,mamicode.com
首页 > 其他好文 > 详细

if-then和if-then-else语句

时间:2014-06-25 08:49:26      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:shell

1.使用if-then语句

    结构化命令中,最基本的类型就是if-then语句。if-then语句有如下格式:

if command
then 
    commands
fi
     如果你在使用其他编程语言的if-then语句,这种形式可能会让你有点困惑。在其他编程语言中,if语句之后的对象是一个等式来测试是TRUE还是FALSE值。如果该命令的退出状态码是0(该命令成功运行),位于then部分的命令就会被执行。如果该命令的退出状态码是其他什么值,那then部分的命令就不会被执行,bash shell会继续执行脚本中的下一个命令。

   这里有简单的例子来解释这个概念:

$ cat if_then.sh 

#!/bin/bash
if test 1 = 2
then
    echo "it worked"
fi

这时运行if_then不会有打印‘it worked‘,改成如下后就会发现,then后面的echo "it worked"将会执行。

#!/bin/bash
if test 1 = 1
then
    echo "it worked"
fi





if-then和if-then-else语句,布布扣,bubuko.com

if-then和if-then-else语句

标签:shell

原文地址:http://blog.csdn.net/funbox/article/details/34108793

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!