`
天梯梦
  • 浏览: 13635396 次
  • 性别: Icon_minigender_2
  • 来自: 洛杉矶
社区版块
存档分类
最新评论

Linux Shell脚本入门教程系列之(九)Shell判断 if else 用法

阅读更多

本文是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=400
b=800

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=400
b=800

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=400
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 Shell系列教程之(九)Shell判断 if else 用法

本文转自:Linux Shell脚本入门教程系列之(九)Shell判断 if else 用法

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    【shell脚本】shell脚本之条件判断if、for与while循环(shell脚本基础学习二)

    【shell脚本】shell脚本之条件判断if、for与while循环(shell脚本基础学习二) shell中的if 单分支 if 条件测试 ;then 命令序列 fi 双分支 if 条件测试 ;then 命令序列1 else 命令序列2 fi 多分支 if 条件测试 ;...

    Shell脚本if else语句小结

    在sh/bash里可不能这么写,如果else分支没有语句执行,就不要写这个else,就像这样: 代码如下: if condition then  command1  command2  …  commandN fi 当然,也可以写成一行(适用于终端命令提示符),像...

    shell脚本学习手册

    Shell教程入门 3 1、shell脚本 3 2、shell脚本实践 4 Shell变量 4 1、使用变量 5 2、只读变量 5 3、删除变量 5 4、变量类型 6 5、shell字符串 6 6、Shell数组 7 7、Shell注释 9 Shell传递参数 9 1、实例 9 Shell数组...

    shell脚本,编写shell脚本

    编写shell脚本: 格式:vi 脚本文件的名字.sh 脚本第一行: #!/bin/bash #!是为了指明当前脚本文件编写完成后,指定的脚本解析器的解析工作 第一种方式: /bin/bash 脚本文件的名字.sh shell脚本:一个能够被执行...

    shell脚本 自己编写 ubuntu下测试运行成功

    (1)判断/home下是否存在一个的目录文件;如果不存在则转(2)继续往下执行;如果存在则判断该目录下是否存在一个.c文件,如果不存在则转第(3)步,如果存在则从第(4)...(6)在shell脚本中运行程序,输出相应内容

    linux shell 编程教程

    linux shell编程 教程大全 目 录 译者序 前言 第一部分 shell 第1章 文件安全与权限 1 1.1 文件 1 1.2 文件类型 2 1.3 权限 2 1.4 改变权限位 4 1.4.1 符号模式 4 1.4.2 chmod命令举例 5 1.4.3 绝对模式 5 1.4.4 ...

    shell编程 创建shell脚本 shell 基础语法 shell流程控制语法 shell函数

    shell编程 创建shell脚本 ①创建第一个shell脚本②运行shell脚本shell 基础语法 ①变量②变量分类与数组③shell传递参数④基本运算符shell流程控制语法 ①if else条件判断②for循环③while循环④until循环⑤case语句...

    Linux学习笔记09 — 超详细shell脚本编程快速入门

    文章目录1.shell简介1)什么是shell2)shell脚本3)运行shell脚本4)shell注释5)shell编写的基本步骤2.shell变量1)命名变量2)使用变量3)变量类型3)变量操作3.shell字符串1)字符串类型2)字符串操作4.shell数组...

    Shell脚本学习笔记

    第1章 BashShell命令 6 1.1 Shell提示符 6 1.2 文件与目录Shell命令 7 1.2.1 更改目录命令(cd) 7 1.2.2 列表命令(ls) 7 1.2.3 操作文件命令 10 1.2.4 目录处理命令 14 1.2.5 查看文件内容命令 15 1.3 监控程序 20 ...

    linux shell中 if else以及大于、小于、等于逻辑表达式介绍

    then….else….fi[ -f “somefile” ] :判断是否是一个文件[ -x “/bin/ls” ] :判断/bin/ls是否存在并有可执行权限[ -n “$var” ] :判断$var变量是否有值[ “$a” = “$b” ] :判断$a和$b是否相等-r file ...

    Linux shell脚本编程if语句的使用方法(条件判断)

    if 语句格式if 条件then Commandelse Commandfi 别忘了这个结尾If语句忘了结尾fitest.sh: line 14: syntax error: unexpected end of fi if 的三种条件表达式 ifcommandthen if 函数then 命令执行成功,等于...

    LINUX与UNIX SHELL编程指南(很全)

    16.1 使用shell脚本的原因 151 16.2 脚本内容 151 16.3 运行一段脚本 152 16.4 小结 153 第17章 条件测试 154 17.1 测试文件状态 154 17.2 测试时使用逻辑操作符 155 17.3 字符串测试 155 17.4 测试数值 156 17.5 ...

    shell 编程指南pdf

    16.1 使用shell脚本的原因 151 16.2 脚本内容 151 16.3 运行一段脚本 152 16.4 小结 153 第17章 条件测试 154 17.1 测试文件状态 154 17.2 测试时使用逻辑操作符 155 17.3 字符串测试 155 17.4 测试数值 156 17.5 ...

    Linux与unix shell编程指南

    16.1 使用shell脚本的原因 151 16.2 脚本内容 151 16.3 运行一段脚本 152 16.4 小结 153 第17章 条件测试 154 17.1 测试文件状态 154 17.2 测试时使用逻辑操作符 155 17.3 字符串测试 155 17.4 测试数值 156 17.5 ...

    Linux shell编程指南

    16.1 使用shell脚本的原因 151 16.2 脚本内容 151 16.3 运行一段脚本 152 16.4 小结 153 第17章 条件测试 154 17.1 测试文件状态 154 17.2 测试时使用逻辑操作符 155 17.3 字符串测试 155 17.4 测试数值 156 17.5 ...

    绝版经典《Linux与UNIX Shell编程指南》

    16.1 使用shell脚本的原因 151 16.2 脚本内容 151 16.3 运行一段脚本 152 16.4 小结 153 第17章 条件测试 154 17.1 测试文件状态 154 17.2 测试时使用逻辑操作符 155 17.3 字符串测试 155 17.4 测试数值 156 17.5 ...

    Shell脚本实现IP地址合法性判断

    做unix/linux下的开发,脚本编写的功力是少不了的,作为shell编程,也是博大精深的一个技术领域,这里为了学习,就写一个简单的判断IP地址是否合法的微型脚本程序,这个小程序也是非常有用的。 IP地址是32位的,可以...

    linux shell流程控制语句实例讲解(if、for、while、case语句实例)

    一、shell条件语句(if用法) if语句结构[if/then/elif/else/fi] 代码如下:if 条件测试语句 then action [elif 条件 action else action ] fi如果对于:条件测试语句不是很清楚,可以参考:linux shell 逻辑运算符...

Global site tag (gtag.js) - Google Analytics