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 ...
随机推荐
- TEXT 8 Ready, fire, aim
TEXT 8 Ready, fire, aim 预备!开火!瞄准!! Feb 16th 2006 From The Economist print edition Foreword:A vice-pr ...
- Mysql count+if 函数结合使用
Mysql count+if 函数结合使用 果林椰子 关注 2017.05.18 13:48* 字数 508 阅读 148评论 0喜欢 1 涉及函数 count函数 mysql中count函数用于统计 ...
- html file 文件批量上传 以及碰到的一些问题提
//javascript 代码 $("#submite").click(function (evt) { var arrayTr = $("#datatables&quo ...
- 自定义worker的方法,及一例
自定义的worker用于处理各种特殊需求. 有网友想用html_json提取雪球网(https://xueqiu.com/)的数据,可是雪球网用了反爬虫技术,网站要求有cookies才能访问到json ...
- mark_2017_2_27
工作总结web_acl 535 git clone “ssh://git@outergit.yonyou.com:49622/esn_web/web_acl.git" 600 git bra ...
- python没有switch,可以用字典来替代
python没有switch,是因为可以用字典来替代,具体方法如下: def add(x,y): print(x+y)def subtraction(x,y): print(x-y)def multi ...
- CSS的浮动(float)
问题:在练习过程中,发现div1浮动后,它下面的div被覆盖住了. 解决方案:清除该div1的浮动. 关于CSS的浮动 1.div是块级元素,独占一行 2.浮动可以理解为让某个div元素脱离标准流,漂 ...
- 利用css和javascript实现简单的计算器
<!doctype html> <html> <head> <!--声明当前页面的编码集--> <meta http-equiv="Co ...
- win7卸载打印机驱动
无法删除的话停止Print Spooler服务 删除PRINTERS文件夹下面的文件 C:\Windows\System32\spool\PRINTERS目录下所有的文件,重新启动服务:print s ...
- np.random.seed()
124.np.random.seed()的作用 陈容喜 关注 2018.01.11 21:36 字数 3 阅读 4460评论 0喜欢 6 今天看到一段代码时遇到了np.random.seed(),搞不 ...