本文是Linux Shell系列教程的第(九)篇,更多shell教程请看:Linux Shell系列教程

判断语句是每个语言都必不可少的关键语法,Shell命令当然也不例外。今天就给大家介绍下Shell判断语句 if else 用法。

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

Shell 有三种 if else格式:

  1. if … fi 格式
  2. if … else … fi 格式
  3. if … elif … else … fi 格式

下面我就分别就这几种格式来为大家详细介绍下。

一、Shell判断语法之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

二、Shell判断语法之 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

执行结果:

a is not equal to b

三、Shell判断语法之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,那么不执行任何语句。

使用举例:

#!/bin/sh

a=
b=800 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 语句也可以写成一行,以命令的方式来运行,像这样:

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

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

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

输出:

The two numbers are equal!

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

好了,今天对于Shell判断(Shell if else)用法就先为大家介绍到这里,还是那句话,大家要多多练习才是。

更多shell教程请看:Linux Shell系列教程


本文由Linux技术达人【daxue】发表在:Linux大学
本文固定链接:Linux Shell系列教程之(九)Shell判断 if else 用法

Linux Shell系列教程之(九)Shell判断 if else 用法的更多相关文章

  1. Linux Shell系列教程之(五)Shell字符串

    本文是Linux Shell系列教程的第(五)篇,更多shell教程请看:Linux Shell系列教程 字符串是Shell编程中最常用最有用的数据类型,今天,Linux大学网就为大家介绍一下在She ...

  2. (转)Linux Shell系列教程之(十四) Shell Select教程

    本文属于<Linux Shell 系列教程>文章系列,该系列共包括以下 18 部分: Linux Shell系列教程之(一)Shell简介 Linux Shell系列教程之(二)第一个Sh ...

  3. Linux Shell系列教程

    学习Linux Shell知识,就来Linux大学网(Linuxdaxue.com)! 本系列适合Linux初学者,属于Linux入门级教程,主要介绍了Shell的分类.语法格式以及脚本的使用和编写格 ...

  4. Linux Shell系列教程之(十六) Shell输入输出重定向

    本文是Linux Shell系列教程的第(十六)篇,更多Linux Shell教程请看:Linux Shell系列教程 Shell中的输出和输入的重定向是在使用中经常用到的一个功能,非常实用,今天就为 ...

  5. Linux Shell系列教程之(十七) Shell文件包含

    本文是Linux Shell系列教程的第(十七)篇,更多Linux Shell教程请看:Linux Shell系列教程 通过文件包含,可以引用其他文件的内容,也可以将复杂内容分开,使程序结构更加清晰. ...

  6. Linux Shell系列教程之(十五) Shell函数简介

    本文是Linux Shell系列教程的第(十五)篇,更多Linux Shell教程请看:Linux Shell系列教程 函数可以将一个复杂功能划分成若干模块,从而使程序结构更加清晰,代码重复利用率更高 ...

  7. Linux Shell系列教程之(十四) Shell Select教程

    本文是Linux Shell系列教程的第(十四)篇,更多Linux Shell教程请看:Linux Shell系列教程 在上一篇文章:Linux Shell系列教程之(十三)Shell分支语句case ...

  8. Linux Shell系列教程之(十三)Shell分支语句case … esac教程

    本文是Linux Shell系列教程的第(十三)篇,更多Linux Shell教程请看:Linux Shell系列教程 分支语句非常实用,基本上高级语言都支持分支语句(python 没有),大多数都使 ...

  9. Linux Shell系列教程之(十二)Shell until循环

    本文是Linux Shell系列教程的第(十二)篇,更多Linux Shell教程请看:Linux Shell系列教程 在上两篇文章Linux Shell系列教程之(十)Shell for循环和Lin ...

随机推荐

  1. python基础教程总结15——7 自定义电子公告板

    1. Python进行SQLite数据库操作 简单的介绍 SQLite数据库是一款非常小巧的嵌入式开源数据库软件,也就是说没有独立的维护进程,所有的维护都来自于程序本身.它是遵守ACID的关联式数据库 ...

  2. 从程序猿到SAP产品经理,我是如何转型的?

    文章作者:Jason Xia(夏建军) Jerry: 今天的文章来自Jason Xia, 我的老同事,和我一样从2007年进入SAP成都研究院工作至今.这篇文章讲述了Jason是如何从一名SAP资深开 ...

  3. 10.1 plan

    1951    [Sdoi2010]古代猪文  Sdoi2010 Contest2   807 1928 1566    [NOI2009]管道取珠       806 1429 2756    [S ...

  4. Lemonade Trade

    4990: Lemonade Trade 时间限制: 1 Sec  内存限制: 128 MB  Special Judge提交: 88  解决: 17[提交][状态][讨论版][命题人:admin] ...

  5. cityspace

    类别的网址:https://blog.csdn.net/u010069760/article/details/77847595 r,g,b:  250 170 160 parking   244 35 ...

  6. SQL Server将列以分隔符分割后存到临时表

    begin if object_id('tempdb..#t') is not null drop table #t; create table #t ( filepath ) ); declare ...

  7. JsonPath 语法 与 XPath 对比

    JsonPath 语法 与 XPath 对比   XPath JSONPath Description / $ the root object/element . @ the current obje ...

  8. 四种UNIX实现

    四种UNIX实现:FreeBSD 5.2.1          Linux 2.4.22           mac OS X 10.3        Solaris 9 ubuntu 属于哪一种呢?

  9. mysql中影响数据库性能的因素讲解

    mysql中影响数据库性能的因素讲解 在本篇文章中我们给大家讲述了mysql中影响性能的因素以及相关知识点内容,有兴趣的朋友参考下 关于数据库性能的故事 面试时多多少少会讲到数据库上的事情,“你对数据 ...

  10. SVN:The working copy is locked due to a previous error (二)

    之前也碰到过这种问题,但是根本问题不同,解决方案不同. 传送门:SVN:The working copy is locked due to a previous error (二) 本次错误如图: 解 ...