shell脚本中的逻辑判断
shell脚本中也可以实现逻辑判断。
案例4:shell脚本中的逻辑判断
如果你学过C或者其他语言,相信你不会对if 陌生,在shell脚本中我们同样可以使用if逻辑判断。在shell中if判断的基本语法为:
1)不带else
if 判断语句; then
command
fi
#! /bin/bash
## author:Xiong Xuehao
## Use if in this script. read -p "Please input a number: " a
if ((a<60));then
echo "you didn't pass this test"
fi
在if1.sh中出现了 ((a<60))这样的形式,这是shell脚本中特有的格式,用一个小括号或者不用都会报错,请记住这个格式,即可。执行结果为:

2)带有else
if 判断语句 ; then
command
else
command
fi
#! /bin/bash
## author:Xiong Xuehao
## Use if in this script. read -p "Please input a number: " a
if ((a<60));then
echo "you didn't pass this test"
else
echo "you pass this test"
fi
执行结果为:

3)带有elif
if 判断语句一 ; then
command
elif 判断语句二; then
command
else
command
fi
#! /bin/bash
## author:Xiong Xuehao
## Use if in this script. read -p "Please input a number: " a
if ((a<60));then
echo "you didn't pass this test"
elif ((a>=60)) && ((a<85));then
echo "you pass this test"
else
echo "Verry Good!"
fi
这里的 && 表示“并且”的意思,当然你也可以使用 || 表示“或者”,执行结果:

以上只是简单的介绍了if语句的结构。在判断数值大小除了可以用”(( ))”的形式外,还可以使用”[ ]” 注意方括号里面需要有空格。但是就不能使用>, < , = 这样的符号了,要使用 -lt (小于),-gt (大于),-le (小于等于),-ge (大于等于),-eq (等于),-ne (不等于)。
#! /bin/bash
## author:Xiong Xuehao
## Use if in this script. read -p "Please input a number: " a
if [ $a -lt 60 ];then
echo "$a lt than 60";
elif [ $a -ge 60 ] && [ $a -lt 85 ];then
echo "you pass this test"
elif [ $a -eq 100 ];then
echo "$a eq than 100. Very Good!"
elif [ $a -ne 0 ];then
echo "$a ne than 100. Your Input error!"
elif [ $a -gt 100 ];then
echo "$a gt than 100. Your Input error!"
else
echo "Your Input error!"
fi
运行结果如图:

案例5:shell脚本中判断档案属性
shell 脚本中if还经常判断关于档案属性,比如判断是普通文件还是目录,判断文件是否有读写执行权限等。常用的也就几个选项:
-e :判断文件或目录是否存在
-d :判断是不是目录,并是否存在
-f :判断是否是普通文件,并存在
-r :判断文档是否有读权限
-w :判断是否有写权限
-x :判断是否可执行
使用if判断时,具体格式为: if [ -e filename ] ; then
#! /bin/bash
## author:Xiong Xuehao
## File properties. read -p "Please input a file or directory: " a
###判断文件或目录是否存在
if [ -e $a ];then
echo "$a The file or directory already exists."
else
echo "$a The file or directory does not exist!"
fi ###判断文件或目录
if [ -d $a ];then
echo "$a The path is a directory and already exists."
elif [ -f $a ];then
echo "$a The path is a file and already exists."
else
echo "$a I don't know!"
fi ###判断权限
if [ -r $a ];then
echo "$a The document has read permission."
else
echo "$a The document does not have read permission!"
fi if [ -w $a ];then
echo "$a The document has write permission."
else
echo "$a The document does not have write permission!"
fi if [ -x $a ];then
echo "$a The document has execution permission."
else
echo "$a The document does not have execution permission!"
fi
执行如图:

案例6:shell脚本中用case逻辑判断
在shell 脚本中,除了用if来判断逻辑外,还有一种常用的方式,那就是case了。具体格式为:
case 变量 in
value1)
command
;;
value2)
command
;;
value3)
command
;;
*)
command
;;
esac
上面的结构中,不限制value的个数,*则代表除了上面的value外的其他值。下面笔者写一个判断输入数值是奇数或者偶数的脚本。
#! /bin/bash
## author:Xiong Xuehao
## Use case in this script. read -p "Please input a number: " n
###判断奇数或者偶数
a=$[$n % 2] case $a in
1)
echo "The remainder is $a. $n The num is odd."
;;
0)
echo "The remainder is $a. $n The num is even."
;;
esac
输入任意一个自然数,除以2得余数 $a 的值或为1或为0,执行结果为:

case脚本常用于编写系统服务的启动脚本,例如/etc/init.d/iptables中就用到了,不妨去查看一下。
shell脚本中的逻辑判断的更多相关文章
- Shell脚本中的逻辑判断、文件目录属性判断、if的特殊用法、case判断
1.Shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi格式2:if 条件; then 语句; else 语句; fi格式3:if …; then … ;elif …; then ...
- shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断
7月12日任务 20.5 shell脚本中的逻辑判断20.6 文件目录属性判断20.7 if特殊用法20.8/20.9 case判断 20.5 shell脚本中的逻辑判断 逻辑判断在shell中随处可 ...
- Linux centosVMware shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断
一.shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi 格式2:if 条件; then 语句; else 语句; fi 格式3:if …; then … ;elif …; th ...
- shell脚本中的逻辑判断 文件目录属性判断 if特殊用法 case判断
case判断 • 格式 case 变量名 in value1) command ...
- shell脚本中常见的一些特殊符号和作用详解
这篇文章主要介绍了shell脚本中常见的一些特殊符号和它的作用详解,总结的很简洁,容易看懂,需要的朋友可以参考下 在编写Shell脚本时,我们需要会用到各种各样的特殊符号,通过这些特殊符号可以使我 ...
- shell脚本中的整数测试
shell脚本中的整数测试 author:headsen chen 2017-10-17 13:58:12 个人原创,转载请注明作者,出处,否则依法追究法律责任 1,test用法:tes ...
- shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量
7月11日任务 20.1 shell脚本介绍20.2 shell脚本结构和执行20.3 date命令用法20.4 shell脚本中的变量 20.1 shell脚本介绍 1.shell脚本语言是linu ...
- Linux centosVMware shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量
一. shell脚本介绍 shell是一种脚本语言 aming_linux blog.lishiming.net 可以使用逻辑判断.循环等语法 可以自定义函数 shell是系统命令的集合 shell脚 ...
- 详解Linux交互式shell脚本中创建对话框实例教程_linux服务器
本教程我们通过实现来讲讲Linux交互式shell脚本中创建各种各样对话框,对话框在Linux中可以友好的提示操作者,感兴趣的朋友可以参考学习一下. 当你在终端环境下安装新的软件时,你可以经常看到信息 ...
- shell脚本中的反引号使用 `
反引号是~的英文切换 在shell脚本中,反引号允许将shell命令的输出赋值给变量. test=`date` 这样shell会执行反引号中的命令.并将执行的结果赋值给变量tests.
随机推荐
- 洛谷P4913【深基16.例3】二叉树深度题解
很简单的二叉树遍历问题,可以用dfs(深度优先搜索)解决. 看到数据范围,最大不超过 \(10^6\) ,可以不开 long long (但我还是习惯性地开了) 接下来上代码: #include< ...
- 七、FreeRTOS学习笔记-中断管理
FreeRTOS学习笔记-中断管理 中断:让CPU打断正常运行的程序,转而去处理紧急的事件(程序) 中断执行机制,可简单概括为三步: 1.中断请求:外设产生中断请求(GPIO外部中断.定时器中断等) ...
- S2P销讯通·CRM-移动的客户关系精细化管理
S2P销讯通·CRM是一款专为医药企业设计的移动客户关系管理软件.该软件安装在手机上,集主数据管理.辖区指标管理.客户管理.SFE管理.OTC动销管理.精细化招商管理.市场活动管理以及流向采集清洗与统 ...
- 怎么实时更新echarts图标数据?
function getData(){ var request . nem XPHLHttpRequest () ; request . open("get",'http://lo ...
- building qtqml requires python
编译Qt5.14.2的qtdeclarative发生错误:building qtqml requires python,系统是ubuntu18.04,安装有python3,修复需要做2步: 1.用na ...
- js 进制转换:十六进制转十进制、十进制转十六进制、十六进制转ASCII码、
因为近期做小程序,蓝牙连接硬件,需要根据module bus通信协议解析数据,用到了很多标题的算法转换,借此总结一下. 十六进制 转 十进制 function hex2dec(hex) { var l ...
- Android 12 适配之 "Android:exported"
Android 12 适配之 "Android:exported" 将 build.gradle 中的 targetSDKVersion 和 compileSdkVersion 改 ...
- ngrx 4: 创建 ngrx 4.x 项目
创建 @ngrx 4.x 项目 @ngrx 4.x 相比上一个版本有了一些变化,该文介绍了如何在 Angular 中集成 @ngrx r.x 实现状态管理. 一.创建基本项目 使用 Angular C ...
- Netty SSL双向验证
一· 快速命令 1.生成ca证书 openssl req -new -x509 -keyout ca.key -out ca.crt -days 36500在本目录得到 ca.key 和 ca.crt ...
- initMySQLPool
package com.be.edge.asset.source; import io.vertx.core.AbstractVerticle; import io.vertx.core.Promis ...