if 语句通过关系运算符判断表达式的真假来决定执行哪个分支。Shell 有三种 if ... else 语句:

  • if ... fi 语句;
  • if ... else ... fi 语句;
  • if ... elif ... else ... fi 语句。

1) if ... else 语句

if ... else 语句的语法:

if [ expression ]
then
Statement(s) to be executed if expression is true
fi

如果 expression 返回 true,then 后边的语句将会被执行;如果返回 false,不会执行任何语句。

最后必须以 fi 来结尾闭合 if,fi 就是 if 倒过来拼写,后面也会遇见。

注意:expression 和方括号([ ])之间必须有空格,否则会有语法错误。

#!/bin/sh
a=
b= #下面两种写法都是正确的,除此之外的写法都是错误的
if [ $a == $b ]
then
echo 'a is equal to b'
fi if [ $a != $b ] ; then
echo 'a is not equal to b'
fi

运行结果:

a is not equal to b

2) if ... else ... fi 语句

if ... else ... fi 语句的语法:

if [ expression ]
then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if expression is not true
fi

如果 expression 返回 true,那么 then 后边的语句将会被执行;否则,执行 else 后边的语句。

举个例子:

#!/bin/sh

a=
b= if [ $a == $b ] ;then
echo "a is equal to b"
else
echo "a is not equal to b"
fi if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi :<<EOF 下面的写法是错误的,请注意
if [ $a == $b ] then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
EOF

执行结果:

a is not equal to b
a is not equal to b

3) if ... elif ... fi 语句

if ... elif ... fi 语句可以对多个条件进行判断,语法为:

if [ expression  ]
then
Statement(s) to be executed if expression is true
elif [ expression ]
then
Statement(s) to be executed if expression is true
elif [ expression ]
then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if no expression is true
fi

哪一个 expression 的值为 true,就执行哪个 expression 后面的语句;如果都为 false,那么不执行任何语句。

举个例子:

#!/bin/sh

a=
b= if [ $a == $b ] ; then
echo "a is equal to b"
elif [ $a -gt $b ] ; then
echo "a is greater than b"
elif [ $a -lt $b ] ; then
echo "a is less than b"
else
echo "None of the condition met"
fi

运行结果:

a is less than b

if ... else 语句也可以写成一行,以命令的方式来运行,像这样:

#!/bin/bash

if test $[*] -eq $[+]; then echo 'The two numbers are euqal!'; fi;
#!/bin/bash

if test $(*) -eq $(+); then echo 'The two numbers are euqal!'; fi;

if ... else 语句也经常与 test 命令结合使用,如下所示:

#!/bin/bash

num1=$[*]
num1_1=$[*]
num2=$[+]
num2_1=$[+]
if test $[num1] -eq $[num2] ; then
echo "The two numbers are euqal!"
else
echo 'The tow numbers are not equal!'
fi if test $num1_1 -eq $num2_1 ; then
echo "The two numbers are euqal!"
else
echo 'The tow numbers are not equal!'
fi

输出:

The two numbers are euqal!
The tow numbers are not equal!

test 命令用于检查某个条件是否成立,与方括号([ ])类似。

Shell if else语句的更多相关文章

  1. (二)shell中case语句、程序传参、while

    2.2.6.1.case语句(1)shell中的case语句和C语言中的switch case语句作用一样,格式有差异(2)shell中的case语句天生没有break,也不需要break,和C语言中 ...

  2. shell编程——if语句【转载】

    (2)shell编程——if语句_macg_新浪博客http://blog.sina.com.cn/s/blog_6151984a0100ekl6.html shell编程——if语句转载 if 语句 ...

  3. shell的case语句简述(shell的流控制)

    shell流控制:http://www.cnblogs.com/yunjiaofeifei/archive/2012/06/12/2546208.html 1.if then else 语句 if t ...

  4. Shell 编程 until语句

    本篇主要写一些shell脚本until语句的使用. 计算1-50的和 #!/bin/bash i=0 s=0 until [ $i -eq 51 ];do let s+=i;let i++ done ...

  5. Shell 编程 循环语句

    本篇主要写一些shell脚本循环语句的使用. for 循环 指定次数 #!/bin/bash for ((i=1;i<=10;i++)) do echo $i done [root@localh ...

  6. Shell 编程 case语句

    本篇主要写一些shell脚本case语句的使用. 字符判断 #!/bin/bash read -p "请输入一个字符:" char case $char in [a-z]|[A-Z ...

  7. Shell 编程 条件语句

    本篇主要写一些shell脚本条件语句的使用. 条件测试 test 条件表达式 [ 条件表达式 ] 文件测试 -d:测试是否为目录(Directory). -e:测试文件或目录是否存在(Exist). ...

  8. Shell case in语句详解

    和其它编程语言类似,Shell 也支持两种分支结构(选择结构),分别是 if else 语句和 case in 语句.在<Shell if else>一节中我们讲解了 if else 语句 ...

  9. shell 的 功能语句--1

    [1]说明性语句 (1)shell 程序和语句 shell 程序由零或多条shell语句构成. shell语句包括三类:说明性语句.功能性语句和结构性语句. 说明性语句: 以#号开始到该行结束,不被解 ...

  10. shell条件测试语句实例-测试apache是否开启

    终于理解了shell条件测试语句"!="和"-n"的用法区别,于是有了如下的shell脚本,做为练习. 第一种方法:测试apache是否开启?字符串测试 #!/ ...

随机推荐

  1. write() ,read();

    int main1(int argc ,char *argv[]){ if(argc < 2 ) return 0; int fd = open(argv[1] , O_RDONLY); if( ...

  2. php ini_set('display_errors', $value)

    正常情况下,在开发模式中,把错误显示出来,方便纠正,但在布署模式中,就得把错误关闭: ini_set('display_errors', 1); // 开启 ini_set('display_erro ...

  3. php计算最后一次,第一次字符串出现位置

    strpos($str, n) 首次,n在str第一次出现位置, strrpos($str, n) 最后一次,n在str最后一次出现位置 strripos区分大小写

  4. PDO操作mysql数据库(一)

    PHP连接mysql数据库: <?php$server = "localhost";$user = "root";$pwd = "123456& ...

  5. php表单发送到邮箱V1.0

    html表单代码: <form action="index.php" name="form" method="POST"> &l ...

  6. TDirectory.Copy复制文件

    描述:复制文件夹以及文件夹下的内容 procedure Copy(const SourceDirName, DestDirName: string) 参数一:源文件夹 参数二:目标文件夹 所在单元:S ...

  7. python入门 第二天笔记

    程序主文件标志if __name__=="__main__": 在程序执行python 1.py 时候 程序1.py __name__ 为 main调用其他文件是,__name__ ...

  8. UIVIewController自定义切换效果-b

      之前介绍动画时提过UIView的转场动画,但是开发中我们碰到更多的viewController的切换,ios中常见的viewcontroller切换有四种:模态视图,导航栏控制器,UITabBar ...

  9. NOIP 2011 提高组 计算系数

    有二项式定理 `\left( a+b\right) ^{n}=\sum _{r=0}^{n}\left( \begin{matrix} n\\ r\end{matrix} \right) a^{n-r ...

  10. webkit javascript

    http://www.infoq.com/cn/news/2013/02/douglas-interview http://blog.csdn.net/horkychen/article/detail ...