本文是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. .net 实现的上传下载,如果是图片就显示上传的图片

    HTML: <div> <input id="fileUpload" type="file" runat="server" ...

  2. 复杂软件的考虑点与UITableView

    对象的要素.组织.整体情况: 对象的生成步骤.生成的时间节点. 考虑UITableview.

  3. Silverlight日记:字符串装换成Path对象

    一,需要动态绑定Path <UserControl x:Class="Secom.Emx2.SL.Common.Controls.EleSafe.Ele.Line" xmln ...

  4. cocostudio的bug(1)

    今天有个女同事问我一个问题,两个cocostudio的ui同时addChild到一个layer上面,高层级的ui设置visible为false,低层级的ui设置的visible设置为true,然后低层 ...

  5. Unity3d 判断物体是否在可见范围内

    unity中自带两个回调函数: void OnBecameVisible()//当物体可见时,回调一次. void OnBecameInvisible()//当物体不可见时,回调一次. 在untiy编 ...

  6. 66. Plus One@python

    Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The ...

  7. matplotlib subplot 子图

    总括 MATLAB和pyplot有当前的图形(figure)和当前的轴(axes)的概念,所有的作图命令都是对当前的对象作用.可以通过gca()获得当前的axes(轴),通过gcf()获得当前的图形( ...

  8. ubuntu14.04搭建LAMP环境(nginx,php,mysql,linux)详解

    最近更换开发环境至ubuntu,整理开发环境和常用软件的安装配置(更新排版) 以下安装过程经过多次操作得出,参照步骤进行操作即可 一.LAMP基本环境搭建 1 切换root账号 sudo su 2,安 ...

  9. mysql替换表中某字段的某值

    UPDATE  `cases`  SET  `case_desc` = replace(`case_desc`, 'src="//tuku-assets.m.jia.com/assets/i ...

  10. IDEA入门学习笔记1:资料收集

    IDEA2018软件下载 :https://mp.weixin.qq.com/s?__biz=MzIwMjE1MjMyMw==&mid=2650200056&idx=1&sn= ...