当前位置:网站首页>Conditional Statements of Shell Programming (2)
Conditional Statements of Shell Programming (2)
2022-08-10 22:00:00 【Guannan cattle x people】
目录
条件测试
条件测试操作
test命令
测试表达式是否成立,若成立返回0,否则返回其他数值
格式1:test 条件表达式
格式2:[ 条件表达式 ]
条件表达式:Judge yes or no
文件测试
[ 操作符 文件或目录 ]
常用的测试操作符
-d:测试是否为目录(Directory)
-e:测试目录或文件是否存在(Exist)
-f:测试是否为文件(File)
-r:测试当前用户是否有权限读取(Read)
-w:测试当前用户是否有权限写入(Write)
-x:测试当前用户是否有权限执行(Excute)
-b:测试是否为设备文件
-c:测试是否为字符设备文件
-s:测试存在且文件大小为空
-L:测试是否为链接文件
执行条件测试操作以后,通过预定义变量$? 可以获得测试命令的返回状态值,从而判断该条件是否成立.
一些操作
第二种格式:
整数值比较
[整数1 操作符 整数2]
常用的测试操作符
-eq:等于(Equal)
-ne:不等于(Not Equal)
-gt:大于(Greater Than)
-lt:小于(Lesser Than)
-le:小于或等于(Lesser or Equal)
-ge:大于或等于(Greater or Equal)
案例一
案例2
字符串比较
格式1:[字符串1 = 字符串2]
[字符串1 != 字符串2 ]
格式2:[-z 字符串]
常用的测试操作符
=:字符串内容相同
!=:字符串内容不同,!号表示相反的意思
-z:字符串内容为空
案例1
案例2
案列3
逻辑测试
格式1:[表达式1] 操作符 [表达式2]
格式2:命令1 操作符 命令2....
常用的测试操作符
-a或&&:逻辑与,“而且”的意思
-o或||:逻辑或,“或者”的意思
!:逻辑否
if语句的结构
单分支结构
Second simple example
Because this Chinese character is so ugly,如何去掉
案例
判断目录是否存在,启动 脚本以后,custom creation/date目录
案例三
遇到问题,没有反应
加个中括号
补充:单个括号( 【 】):必须要有空格 两个字符或数字之间的比较左右必须有空格
条件判断: test一种形式, 【】中间只能使用=和!=比较字符串, 如果使用<、>需要进行转义\.
Then use it if you compare numbers in the middle-lt等符号,不能使用\<比较数字,It will be treated as a string
两个括号(【【】】),Can be used to process logical commands,也可以处理字符串是否相等,且使用<、>不用转义符
双分支结构
案例
案例2
例3
多分支结构
案例
案列2
嵌套if语句
例1.判断httpd服务有没有启动
If 判断是否启动
嵌套iF 如果启动------输出已启动
如果没启动----判断是否安装---如果安装---启动
如果没安装----安装---如果安装成功---启动
如果安装不成功-----报错
ps aux | grep httpd | grep -v grep //查询过滤httpd服务
if [ $? -ne 0 ];then //有等于0,没有不等于0
if [ "$(rpm -q httpd)"=="未安装软件包 httpd " ];then //If not0,Indicates that the service is not available ,则表示未安装
注意:There can be no spaces on either side of the equal sign
echo “正在ApachePlease wait for service..........”
yum -y install httpd //则下载httpd服务
systemctl start httpd //开启服务
Else //Otherwise, the installation package already exists,Just turn it on directlyhttpd服务
systemctl start httpd
fi
else
echo "httpd is running" //如果为0,Indicates that the service is running
Fi
1
移除httpdExecute the script after the package
case语句结构
格式:
case 变量名称 in
模式1)
命令序列
;;
模式2)
程序段
;;
*)
不包含第一个变量内容与第二个变量内容的其他程序执行段
默认程序段
;;
esac
case行首必须以单词“in”,每一模式必须以单个右括号")"结束
双分号";;"表示命令序列的结束
模式字符串中,可以用方括号表示一个连续的范围,如"[0-9]";还可以使用竖杠|表示或,比如a|b
最后的)表示默认模式,其中的相当于通配符
case多分支语句
编写apache启动服务脚本
边栏推荐
- 《mysql 从入门到内卷再到入土》——Mysql基础 学习笔记目录
- PROCEDURE :存储过程结构——《mysql 从入门到内卷再到入土》
- Huawei router clock near the drainage experiment (using stream strategy)
- 带你一文读懂SaaS版多租户商城系统对多品牌企业的应用价值
- C # Hex file transfer skills necessary article 】 【 bin file code implementation
- 地理探测器Geodetector软件的下载、应用与结果解读
- HighTec shortcut keys (Keys) setting location
- 阿里巴巴、蚂蚁集团推出分布式数据库 OceanBase 4.0,单机部署性能超 MySQL
- RADIUS Authentication Server Deployment Costs That Administrators Must Know
- 黑猫带你学Makefile第12篇:常见Makefile问题汇总
猜你喜欢
Redis 性能影响 - 异步机制和响应延迟
C. Even Picture
着力提升制造业核心竞争力,仪器仪表产业迎高质量发展
Live Classroom System 08 Supplement - Tencent Cloud Object Storage and Course Classification Management
PROCEDURE :存储过程结构——《mysql 从入门到内卷再到入土》
APP UI自动化测试常见面试题,或许有用呢~
异常的了解
The use of TortoiseSVN little turtle
Using SylixOS virtual serial port, serial port free implementation system
apr_thread使用内存之谜
随机推荐
Labelme-5.0.1 version edit polygon crash
LeetCode-36-二叉搜索树与双向链表
Before implementing MES management system, these three questions to consider
3D model reconstruction of UAV images based on motion structure restoration method based on Pix4Dmapper
Bedtime story | made a Bitmap and AST length system configuration
c语言之 练习题1 大贤者福尔:魔法数,神奇的等式
shell (text printing tool awk)
About DataFrame: Processing Time
地理探测器Geodetector软件的下载、应用与结果解读
【PCBA solution】Electronic grip strength tester solution she'ji
智能方案设计——智能跳绳方案
Exploration and practice of the "zero trust" protection and data security governance system of the ransomware virus of Meichuang Technology
ACM解题笔记——HDU 1401 Solitaire(DBFS)
这些不可不知的JVM知识,我都用思维导图整理好了
【nvm】【node多版本管理工具】使用说明和踩坑(exit status 1)
Live Classroom System 09--Tencent Cloud VOD Management Module (1)
什么是Jmeter?Jmeter使用的原理步骤是什么?
力扣215题,数组中的第K个最大元素
C. Social Distance
图数据库(Neo4j)入门