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 ...
随机推荐
- Kotlin语言学习笔记(5)
委托模式(Delegation) 类的委托 interface Base { fun print() } class BaseImpl(val x: Int) : Base { override fu ...
- xshell分隔符
1.分隔符设置 \ :;`!@#$%^&()+|[]{}'",<>? 2.左键点击选中,右键点击复制
- IIS7配置下载apk以及目录浏览
IIS7为了增加安全性,如果需要禁止目录浏览.只需要按下面的步骤执行就可以 1.选择站点:2.选择功能视图:3.选择IIS下面的目录浏览:4.在右上角的操作中选择“打开功能”:5.选择右边的禁用. 今 ...
- css3文字截断
width:200px; height:14px; overflow:hidden; white-space:nowrap; text-overflow:ellipsis; text-overflow ...
- 64位windows+32位JDK8+32位eclipse是可以的
- Dell 服务器安装方法介绍
大家都知道dell服务器在安装windows系统时都需要有raid卡驱动的加载才可以人道服务器硬盘,下面来介绍一下dell服务器raid卡驱动的加载和系统的安装: 方法一: 使用dell服务器自带的 ...
- ccf认证模拟题之三---最大的矩形
问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个矩形的高度是hi.这n个矩形构成了一个直方图.例如,下图中六个矩形的高度就分别是3, 1, 6, 5, 2, 3 ...
- day10:vcp考试
Q181. An administrator is deploying ESXi 6.x hosts using Auto Deploy and wants the image profile to ...
- [leetcode]139. Word Break单词能否拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- LocalBroadcastManager 的使用
一.使用本地广播发送一条广播(本例为自己发送自己接收,本地广播也可以是其他应用接收)然后接收到广播时回调Receiver类中的回调方法onReceive()在此方法中自定义发出通知 代码 packag ...