Shell编程-06-Shell中的if语句
在任何一门语言中,判断语句总是少不了,今天来学习一下Shell中的if语句。
基本语法
单分支情况
- 第一种语法
if <条件表达式>
then
语句
fi
- 第二种语法
if <条件表达式>;then
语句
fi
其中条件表达式部分可以是test、[]、[[]]和(())等条件表达式。以上两种格式,可根据自己实际情况选择一种即可。
双分支情况
if <条件表达式>
then
语句
else
语句
fi
多分支情况
if <条件表达式>
then
语句
elif <条件表达式>
then
语句
elif <条件表达式>
then
语句
else
语句
fi
在多分支的if中,每个elif中后均需要带有then
分支嵌套情况
if <条件表达式>
then
if <条件表达式>
then
语句
fi
fi
在以上的写法注意缩进,方便阅读
建议在一个if嵌套不要超过三层
if与条件表达式语法
在前面讲过各个条件测试表达式,如test、[]、[[]]和(())等条件表达式,如下所示:
- 1、test表达式
if test <表达式>
then
语句
fi
- 2、[]表达式
if [ <表达式> ]
then
语句
fi
- 3、[ [ ] ]表达式
if [[ <表达式> ]]
then
语句
fi
- 4、(( ))表达式
if (( <表达式> ))
then
语句
fi
- 5、命令表达式
if 命令
then
语句
fi
if示例
1、if示例:判断文件是否且为普通文件
[root@localhost ~]# [ -f /etc/passwd ] && echo true || echo false
true
[root@localhost ~]# test -f /etc/passwd && echo true || echo false
true
与以下写法等效
[root@localhost Test]# cat if.sh
#!/bin/bash
if [ -f "$1" ]
then
echo true
else
echo false
fi
if test -f "$2"
then
echo true
else
echo false
fi
[root@localhost Test]# bash if.sh /etc/passwd /etc/hostssss
true
false
2、if示例:比较输入数字的大小
[root@localhost Test]# cat compareNum.sh
#!/bin/bash
a=$1
b=$2
echo "Inputed number is:" ${a} ${b}
if [ $# -ne 2 ]
then
echo "input number must be 2 number."
exit 2
fi
expr $a + 2 &> /dev/null # 检查是否为整数
resulta=$?
expr $b + 2 &> /dev/null # 检查是否为整数
resultb=$?
if [ $resulta -eq 0 -a $resultb -eq 0 ] # 判断检查结果
then
if [ $a -gt $b ]
then
echo "$a > $b"
elif [ $a -lt $b ]
then
echo "$a < $b"
elif [ $a -eq $b ]
then
echo "$a = $b"
else
echo "error"
fi
else
echo "please check your input"
fi
[root@localhost Test]# bash compareNum.sh 1 # 输入一个数字
Inputed number is: 1
input number must be 2 number.
[root@localhost Test]# bash compareNum.sh a b # 输入字母
Inputed number is: a b
please check your input
[root@localhost Test]# bash compareNum.sh 900 89 # 输入两个数字
Inputed number is: 900 89
900 > 89
[root@localhost Test]# bash compareNum.sh 89 900
Inputed number is: 89 900
89 < 900
[root@localhost Test]# bash compareNum.sh 900 900
Inputed number is: 900 900
900 = 900
本文同步在微信订阅号上发布,如各位小伙伴们喜欢我的文章,也可以关注我的微信订阅号:woaitest,或扫描下面的二维码添加关注:

Shell编程-06-Shell中的if语句的更多相关文章
- shell编程系列7--shell中常用的工具find、locate、which、whereis
shell编程系列7--shell中常用的工具find.locate.which.whereis .文件查找之find命令 语法格式:find [路径] [选项] [操作] 选项 -name 根据文件 ...
- shell编程系列6--shell中的函数
shell编程系列6--shell中的函数 .函数介绍 linux shell中的函数和大多数编程语言中的函数一样 将相似的任务或者代码封装到函数中,供其他地方调用 语法格式 第一种格式 name() ...
- 【Shell编程】Shell程序设计
1.Shell简介 作为Linux灵感来源的Unix系统最初是没有图形化界面的,所有的任务都是通过命令行来实现的.因此,Unix的命令行系统得到了很大的发展,逐步成为一个功能强大的系统. Sh ...
- shell编程01—shell基础
01.学习shell编程需要的知识储备 1.vi.vim编辑器的命令,vimrc设置 2.命令基础,100多个命令 3.基础.高端的网络服务,nfs,rsync,inotify,lanmp,sersy ...
- Linux shell编程02 shell程序的执行 及文件权限
第一个shell脚本 1. shell编程的方式 交互式shell编程 非交互式shell编程:执行的语句存放到一个文件 shell脚本:可以任意文件名,建议扩展名为sh 2. ...
- 【Shell编程】Shell基本语法
Shell 语法 Shell程序设计作为一种脚本语言,在Linux系统中有广泛的应用,本文记录了关于Shell程序设计的基础语法知识和常用命令,方便查询,熟练使用shell也需要经常实践,这对于完 ...
- Shell编程(二)——shell的基础知识及常用命令
shell的基础知识 一.bash有以下特点: 1.记录命令历史 2.指令和文件名补全 3.别名 alias rm='rm -i' 4.通配符 * 0个或多个字符 ?匹配一个字符 5 输入输出重定向 ...
- Shell编程中括号判断中赋值语句和判断语句
#!/bin/bash declare var="xxx" # without space and use one = #1.judge whether the assignmen ...
- Linux - 简明Shell编程06 - 循环语句(Loop)
脚本地址 https://github.com/anliven/L-Shell/tree/master/Shell-Basics 示例脚本及注释 #!/bin/bash # for循环 for fil ...
- Linux shell编程 4 ---- shell中的循环
1 for循环 1 for语句的结构 for variable in values; do statement done 2 for循环通常是用来处理一组值,这组值可以是任意的字符串的集合 3 for ...
随机推荐
- Java发送HTTPS请求
前言 上篇文章介绍了 java 发送 http 请求,大家都知道发送http是不安全的 .我也是由于对接了其他企业后总结了一套发送 https的工具.大家网上找方法很多的,但是可不是你粘过来就能用啊, ...
- SpringBoot配置文件YML 注意事项
YML读取注意事项 使用YML时遇到的坑: 最近在做项目时,遇到了一些在读取YML配置时发生的问题,在这里写一并写下来,希望给自己以及大家一个提示,能尽量避免在读取配置文件时发生这些错误,给开发带来不 ...
- .net 委托的用法
定义了两个委托 //Func有返回值:Action无返回值.两个委托 Func<int,int> f= a =>a+1;//参数,返回值: int reslut=f(5);//6
- byte,short,int,long数据之间的倍数关系
基本数据类型 byte = -128和127------------------------------------------------------------2的8次方,1个字节 shor ...
- js:Date格式化
将Date类型格式化为"yyyy/MM/dd HH:mm:ss" 函数代码如下: //Date的prototype 属性可以向对象添加属性和方法. Date.prototype.F ...
- asp.net后台解析JSON,并将值赋给对象
示例代码如下: using System; using System.Collections.Generic; using System.Web.Script.Serialization; publi ...
- 使用DW工具给图片添加热点MAP
一.准备一张图片. 准备一张需要给不同区域添加不同热点的图片. 二.插入图片: 打开Dreamweaver,新建一个网页,将图片插入到页面中. 三.找到地图工具: 单击鼠标左键点击图片,这时候 ...
- [leetcode]215. Kth Largest Element in an Array 数组中第k大的元素
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- win8.1下cocos2d-x 3.x环境搭建
Win8.1下Cocos2d-x 3.4环境搭建 第一步: 需要下载的:(Windows 64位系统下环境搭建) Ant apache-ant-1.9.4-bin.zip NDK androi ...
- JavaScript 中的 NaN 和 isNaN
1.NaN NaN 即 Not a Number , 不是一个数字.那么 NaN 到底是什么呢? 在 JavaScript 中,整数和浮点数都统称为 Number 类型 .除此之外,Number 类型 ...