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

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

1) if ... else 语句

if ... else 语句的语法:

  1. if [ expression ]
  2. then
  3. Statement(s) to be executed if expression is true
  4. fi

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

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

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

  1. #!/bin/sh
  2. a=
  3. b=
  4.  
  5. #下面两种写法都是正确的,除此之外的写法都是错误的
  6. if [ $a == $b ]
  7. then
  8. echo 'a is equal to b'
  9. fi
  10.  
  11. if [ $a != $b ] ; then
  12. echo 'a is not equal to b'
  13. fi

运行结果:

  1. a is not equal to b

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

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

  1. if [ expression ]
  2. then
  3. Statement(s) to be executed if expression is true
  4. else
  5. Statement(s) to be executed if expression is not true
  6. fi

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

举个例子:

  1. #!/bin/sh
  2.  
  3. a=
  4. b=
  5.  
  6. if [ $a == $b ] ;then
  7. echo "a is equal to b"
  8. else
  9. echo "a is not equal to b"
  10. fi
  11.  
  12. if [ $a == $b ]
  13. then
  14. echo "a is equal to b"
  15. else
  16. echo "a is not equal to b"
  17. fi
  18.  
  19. :<<EOF 下面的写法是错误的,请注意
  20. if [ $a == $b ] then
  21. echo "a is equal to b"
  22. else
  23. echo "a is not equal to b"
  24. fi
  25. EOF

执行结果:

  1. a is not equal to b
  2. a is not equal to b

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

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

  1. if [ expression ]
  2. then
  3. Statement(s) to be executed if expression is true
  4. elif [ expression ]
  5. then
  6. Statement(s) to be executed if expression is true
  7. elif [ expression ]
  8. then
  9. Statement(s) to be executed if expression is true
  10. else
  11. Statement(s) to be executed if no expression is true
  12. fi

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

举个例子:

  1. #!/bin/sh
  2.  
  3. a=
  4. b=
  5.  
  6. if [ $a == $b ] ; then
  7. echo "a is equal to b"
  8. elif [ $a -gt $b ] ; then
  9. echo "a is greater than b"
  10. elif [ $a -lt $b ] ; then
  11. echo "a is less than b"
  12. else
  13. echo "None of the condition met"
  14. fi

运行结果:

  1. a is less than b

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

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

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

  1. #!/bin/bash
  2.  
  3. num1=$[*]
  4. num1_1=$[*]
  5. num2=$[+]
  6. num2_1=$[+]
  7. if test $[num1] -eq $[num2] ; then
  8. echo "The two numbers are euqal!"
  9. else
  10. echo 'The tow numbers are not equal!'
  11. fi
  12.  
  13. if test $num1_1 -eq $num2_1 ; then
  14. echo "The two numbers are euqal!"
  15. else
  16. echo 'The tow numbers are not equal!'
  17. fi

输出:

  1. The two numbers are euqal!
  2. 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. 相看系统中用户的信息 passwd, shadow

    用用户的信息都保存在 etc/passwd 和 etc/shadow 文件中,其中 shadow 保存的是经过加密码的 能过 cat etc/passwd 和 cat etc/shadow 来查看相关 ...

  2. demo_06Canvas

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. javaScript & jquery完美判断图片是否加载完毕

    好久没写东西了,正好最近因为工作需要,写了一个瀑布流异步加载的程序. 今天就不谈瀑布流,来谈一下关于load的问题. ----------------------------------------- ...

  4. xml转array

    1.字串 $xml = simplexml_load_string($data);$array = json_decode(json_encode($xml),TRUE); 2.文件$xml = si ...

  5. 快速设置IP的脚本

    @echo off cls ::set NAME="本地连接" set NAME="无线网络连接" set IP=192.168.1.55 set MASK=2 ...

  6. 解析sql中的表名

    最近的项目需求中需要解析sql得表名,由于只需要表名我觉得应该用相对粗暴一点的方式来解析 初步思路: 1.转义字符:去除两个引号连在一起的 2.字符串: 去除所有被引号包裹的 3.括号:识别括号处理 ...

  7. [日语歌词] If

    原唱:西野カナ (にしのカナ) 作词:西野カナ/GIORGIO 13 作曲:GIORGIO CANCEMI 1.单词表 仮名 漢字 ひ 日 あめ 雨 や 止 ちがい 違い とおり 通り じかん 時間 ...

  8. hdu 4751

    一道很简单的题,不过在比赛的时候没有写出来: 刚刚看到这个题,我以为是一个图论题,后来发现其实就是一个暴力的题: 用bfs,因为一个人与他不认识的人肯定不会在一个集合,如果判断出现冲突则分配失败,否则 ...

  9. .h头文件、 .lib库文件、 .dll动态链接库文件之间的关系

    转自.h头文件. .lib库文件. .dll动态链接库文件之间的关系 h头文件作用:声明函数接口 dll动态链接库作用:含有函数的可执行代码 lib库有两种: (1)静态链接库(Static Liba ...

  10. Rotate

    hdu4998:http://acm.hdu.edu.cn/showproblem.php?pid=4998 题意:给你n个点,以及绕每个点旋转的弧度.然后,问你经过这n次旋转,平面中的点总的效果是相 ...