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 ...
随机推荐
- 把AVI存在资源中用TAnimate播放
Animate1.RESName := 'About'; Animate1.Active := True;
- SpringMVC TaskExecutor线程池
一.配置jdbc.properties添加: #------------ Task ------------ task.core_pool_size=5 task.max_pool_size=50 t ...
- 部分真验货客户未取进FP IN_SALES_ORDER表有数据,前台规划页面没显示
描述:部分真验货客户未取进FP,检查发现IN_SALES_ORDER表有数据630\600\610行项目数据,但前台只显示630数据,600和610前台没有显示 1.查看IN_SALES_ORDER表 ...
- AtomEye的使用
网易博客粗略地在转载的基础上对AtomEye补充了概述: AtomEye: Atomistic configuration viewer developed by J. Li. This progra ...
- 132. Palindrome Partitioning II (String; DP)
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【校招面试 之 C/C++】第12题 C++ 重载、重写和重定义
1.成员函数重载特征: a.相同的范围(在同一个类中): b.函数名字相同: c.参数不同(参数个数不同或者参数类型不同,但是返回值不同不能使重载): d.virtual关键字可有可无. 2.重写 ...
- go实现的简易TCP的客户端和服务器
今天介绍golang版本的通信基础:基于TCP的客户端和服务器实现,参考书籍:The Way To Go 那时学习java的时候也是做过通信的,当时是socket编程,服务器监听某一个端口,然后客户机 ...
- PAT 1084 外观数列(20)(代码+思路+推荐测试用例)
1084 外观数列(20 分) 外观数列是指具有以下特点的整数序列: d, d1, d111, d113, d11231, d112213111, ... 它从不等于 1 的数字 d 开始,序列的第 ...
- 750A New Year and Hurry
A. New Year and Hurry time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 680C. Bear and Prime 100 数学
C. Bear and Prime 100 time limit per test:1 second memory limit per test:256 megabytes input:standar ...