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 和方括号([ ])之间必须有空格,否则会有语法错误。

举个例子:

  1. #!/bin/sh
  2. a=10
  3. b=20
  4. if [ $a == $b ]
  5. then
  6. echo "a is equal to b"
  7. fi
  8. if [ $a != $b ]
  9. then
  10. echo "a is not equal to b"
  11. 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 后边的语句。

举个例子:

  1. #!/bin/sh
  2. a=10
  3. b=20
  4. if [ $a == $b ]
  5. then
  6. echo "a is equal to b"
  7. else
  8. echo "a is not equal to b"
  9. fi

执行结果:

a is not equal to b

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

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

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

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

举个例子:

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

运行结果:

a is less than b

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

  1. if test $[2*3] -eq $[1+5]; then echo 'The two numbers are equal!'; fi;

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

  1. num1=$[2*3]
  2. num2=$[1+5]
  3. if test $[num1] -eq $[num2]
  4. then
  5. echo 'The two numbers are equal!'
  6. else
  7. echo 'The two numbers are not equal!'
  8. fi

输出:

The two numbers are equal!

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

【Shell脚本学习16】Shell if else语句的更多相关文章

  1. shell脚本学习—条件测试和循环语句

    条件测试 1. 条件测试:test [ 命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假, 则命令的Exit Status为1(注意与 ...

  2. 转 shell脚本学习指南

    shell脚本学习指南 以下八点不敢说就能成为你shell脚本学习指南de全部,至少可以让你编写出可靠的shell脚本. 1. 指定bashshell 脚本的第一行,#!之后应该是什么?如果拿这个问题 ...

  3. Shell脚本学习指南笔记

    Shell脚本学习指南 作者:Danbo 2015-8-3 脚本编程语言与编译型语言的差异 许多中型.大型的程序都是用编译型语言写的,例如:C.C+.Java等.这类程序只要从源代码(Source C ...

  4. shell脚本学习之6小时搞定(1)

    shell脚本学习之6小时搞定(1) 简介 Shell是一种脚本语言,那么,就必须有解释器来执行这些脚本. Unix/Linux上常见的Shell脚本解释器有bash.sh.csh.ksh等,习惯上把 ...

  5. 笔记——shell脚本学习指南

    <shell脚本学习指南>机械工业出版 ISBN 987-7-111-25504-8 第2章 2.4 初级陷阱 1.当今的系统,对#!这一行的长度限制从63到1024个字符都有,尽量不要超 ...

  6. Shell 脚本学习资料搜集

    Shell文档 ChinaUnix上大神“網中人”总结的Shell十三问,强烈推荐,这本书讲得比较精炼,而且都是一些Shell学习中容易把握不住的一些细节难点.每一问都写得非常精彩.ChinaUnix ...

  7. 学习笔记之Shell脚本学习指南 & sed与awk & 正则表达式

    正则表达式_百度百科 http://baike.baidu.com/link?url=ybgDrN2WQQKN64_gu-diCqdeDqL8LQ-jiQ-ftzzPaNUa9CmgBRDNnyx50 ...

  8. Shell脚本学习 - 运算符

    继续shell脚本学习.上一篇是基本数据类型和语法的总结,这一篇是运算相关的操作. 运算符 bash不支持简单的数学计算,需要依赖其他命令实现. expr可以代为实现. # 表达式一般这么写 ` + ...

  9. Shell脚本学习 - 基本内容以及数据格式

    为了捞取日志,自己用python写了一些东西,大致套路就是读取写入文件的操作,放到linux上跑.实际使用时发现要操作的文件有时比较大,直接打开文件找需要的东西可能会有一些效率问题.所以学习一下she ...

随机推荐

  1. c语言typedef的用法-解惑阿!很多天书般的东西解释的不错(转)

    转自(http://www.cnblogs.com/wchhuangya/archive/2009/12/25/1632160.html) 一.基本概念剖析 int* (*a[5])(int, cha ...

  2. Java环境搭建及语言基础

    JRE和JDK JRE:Java Runtime Environment,java运行环境. JDK:Java Development Kit,java开发工具包. 使用JDK开发java程序,交给J ...

  3. .NET文件上传的大小限制配置

    <system.web>  <!--maxRequestLength单位是Kb-->  <httpRuntime maxRequestLength="20971 ...

  4. Rank() over(partition ... 分组统计的实例

    USE [NanFeng]GO/****** Object: StoredProcedure [dbo].[st_MES_RptMaterilSum] Script Date: 04/18/2016 ...

  5. VC中常用的宏[转]

    我们在VS环境中开发的时候,会遇到很多宏定义,这些宏可以应用到代码中,或用于编译.工程选项等设置,总之是我们开发中必不可少的工具,有必要做一个总结.有些宏是C/C++定义的,有些宏是VC环境预定义的. ...

  6. non-manifold Mesh(非流形网格)

    三角网格曲面中,大多的算法基于流形网格manifold mesh,其定义如下: 1)Each edge is incident to only one or two faces: 一条网格边为一个或两 ...

  7. (剑指Offer)面试题21:包含min函数的栈

    题目: 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数. 在该栈中,调用min,push,pop的时间复杂度都是O(1) 思路: 1.除了原来的栈s,增加一个辅助栈s_min,用 ...

  8. WEB前端性能优化小结

    转:http://www.gafish.net/archives/1514 对前端开发工程师来说,前端性能优化的重要性是不言而喻的,最为大家所知的是YSLOW的23条优化规则,在我的理解中,性能优化不 ...

  9. Android常见工具类封装

    MD5加密 import android.annotation.SuppressLint; import java.security.MessageDigest; public class MD5 { ...

  10. Codeforces Round #339 (Div. 2) B. Gena's Code 水题

    B. Gena's Code 题目连接: http://www.codeforces.com/contest/614/problem/B Description It's the year 4527 ...