shell 条件测试详解

news2024/10/10 12:21:40

目录

shell条件测试

一,条件测试的基本语法

         1,test

2,[  ]

3,[[  ]]

二,文件测试表达式

1,判断目录是否存在:

2,判断文件file1是否有写的权限,结果为有

3,判断文件file1是否有执行的权限

三,逻辑运算符: &&(与), ||(或), !(非)

1,条件1(命令1) && 条件2(命令2) -》 条件1为假,条件2不执行

                                                             条件1为真,条件2执行

2,条件1(命令1) || 条件2(命令2) -》 条件1为真,条件2不执行

                                                          条件1为假,条件2执行

练习:

四,字符串测试表达式

五,整数测试表达式

六,逻辑操作符 (在里面使用)

1, [  ]

2,test

3,[[   ]]

实验1:

实验2:


shell条件测试

        为了能够正确处理Shell程序运行过程中遇到的各种情况,Linux Shell提供了一组测试运算符。通过 这些运算符,Shell程序能够判断某种或者几个条件是否成立。条件测试在各种流程控制语句,例如判断 语句和循环语句中发挥了重要的作用,所以,了解和掌握这些条件测试是非常重要的。

一,条件测试的基本语法

        在shell程序中,用户可以使用测试语句来测试指定的条件表达式的条件的真或假。当指定的条件为 真时,整个条件测试的返回值为0;反之,如果指定的条件为假,则条件测试语句的返回值为非0值。 (shell中真0,假非0)

 1test

[root@wangjingjing 0131]# touch file1

[root@wangjingjing 0131]# test -a file1

[root@wangjingjing 0131]# echo $?

0

[root@wangjingjing 0131]# [ -a file1 ]

[root@wangjingjing 0131]# echo $?

0

2,[  ]

[root@wangjingjing 0131]# [ -a file2 ]

[root@wangjingjing 0131]# echo $?

1

3,[[  ]]

[root@wangjingjing 0131]# [[ -a file1 ]]

[root@wangjingjing 0131]# echo $?

0

二,文件测试表达式

如何在一行上执行两个命令

命令1  &&  命令2

命令1;命令2

小练习:

[root@wangjingjing ~]# cd shell_code23/

[root@wangjingjing shell_code23]# mkdir 0131

[root@wangjingjing shell_code23]# cd 0131

[root@wangjingjing 0131]# ls

[root@wangjingjing 0131]# test -a file1

[root@wangjingjing 0131]# echo $?

1

[root@wangjingjing 0131]#

1,判断目录是否存在:

[root@wangjingjing 0131]# test -d dir1

[root@wangjingjing 0131]# echo $?

1

创建文件dir1,判断dir1是否为目录,结果为否

[root@wangjingjing 0131]# touch dir1

[root@wangjingjing 0131]# test -d dir1

[root@wangjingjing 0131]# echo $?

1

创建目录dir1后再去判断dir1是否为目录,结果为是

[root@wangjingjing 0131]# mkdir dir1

[root@wangjingjing 0131]# test -d dir1

[root@wangjingjing 0131]# echo $?

0

2,判断文件file1是否有写的权限,结果为有

[root@wangjingjing 0131]# [ -w file1 ]

[root@wangjingjing 0131]# echo $?

0

3,判断文件file1是否有执行的权限

[root@wangjingjing 0131]# [ -x file1 ]

[root@wangjingjing 0131]# echo $?

1

也可以用;把两条命令隔开

[root@wangjingjing 0131]# [ -x file1 ]; echo $?

1

注意:如果测试的文件路径是用变量来代替,变量一定要加引号

三,逻辑运算符: &&(与), ||(或), !(非)

1,条件1(命令1) && 条件2(命令2) -》 条件1为假,条件2不执行

                                                             条件1为真,条件2执行

&&:如果条件1为假,条件2不执行

[root@wangjingjing 0131]# test -e file3 && ls -l

[root@wangjingjing 0131]# test -e file1 && ls -l

 

 

2,条件1(命令1) || 条件2(命令2) -》 条件1为真,条件2不执行

                                                          条件1为假,条件2执行

||:如果条件1为真,条件2不执行

[root@wangjingjing 0131]# test -e file3 || ls -l

 

如果第一个条件就可以决定当前的整个条件表达式的真假,那么后面的就不执行了

练习:

让用户输入一个文件名,并做如下判断

(1)如果用户输入的文件为空时显示:you must input a filename,并中断程序。

(2)如果用户输入的文件不存在时,显示the file do not exist,并中断程序

(3) 如果文件存在,判断该文件类型和执行者对改文件所拥有的权限。

1,打开文件file_stst.sh

[root@wangjingjing 0131]# vim file_test.sh

2,编辑保存并退出

#用户输入一个文件名(路径+文件名)

read -p "please input a filename: " filename

#文件名为空时,输出you must input a filename,并退出,状态码为111

test -z "$filename" && echo "you must input a filename" && exit 111    

#如文件不存在时,显示the file do not exist,并中断程序,状态码222

test ! -e "$filename" && echo "the file $filename do not exist" && exit 222

#如果文件存在,判断该文件类型和执行者对改文件所拥有的权限。

#普通文件输出regulare file

[ -f "$filename" ] && echo "regulare file"

#目录文件输出directory

[ -d "$filename" ] && echo "directory"

#可读reable

[[ -r "$filename" ]] && echo "radable"

#可写writable

[[ -w "$filename" ]] && echo "writable"

#可执行executable

[[ -x "$filename" ]] && echo "executable"

3,重新加载文件,测试

[root@wangjingjing 0131]# bash file_test.sh

please input a filename:

you must input a filename

[root@wangjingjing 0131]# echo $?

111

[root@wangjingjing 0131]# bash file_test.sh

please input a filename: file 3

the file file 3 do not exist

[root@wangjingjing 0131]# bash file_test.sh

please input a filename: file1

regulare file

radable

writable

四,字符串测试表达式

test -n "" && echo true || echo false 

若第一个条件为真  echo true

若第一个条件为假  echo false 

字符串比较,比ascii码,从第一个字符开始比较

五,整数测试表达式

注意:

=和!=也可在[]中作比较时使用,在[]中也可使用>和<符号,但需要使用反斜线转义,有时不转译虽然语

法不会报错,但是结果可能会不对;

在[[]]中也可使用包含-gt和-lt的符号,不建议使用;

比较符号两端也要有空格。

[]以及test中使用的比较符号   用字母

在(())只能比较整数和[[]]中使用的比较符号  用符号

[],test整数比较用字母,字符串比较用符号

数字的比较:[],test

字符串的比较  [],test,[[]],

字符串比较用符号:

test:

[root@wangjingjing 0131]# test 5 > 44 && echo true || echo false

true

[ ]: 

[root@wangjingjing 0131]# [ 5 > 44 ] && echo true || echo false

true

数字比较用字母:

test:

[root@wangjingjing 0131]# test 5 -gt 44 && echo true || echo false

false

 [ ]: 

[root@wangjingjing 0131]# [ 5 -gt 44 ] && echo true || echo false

false

test 比较字符串与test比较数字:

[root@wangjingjing 0131]# test 2 \< 3 && echo true || echo false

true

[root@wangjingjing 0131]# test 2 -lt 3 && echo true || echo false

true

[root@wangjingjing 0131]# test 2 -gt 3 && echo true || echo false

false

[root@wangjingjing 0131]# test 2 \> 3 && echo true || echo false

false

[ ]比较字符串和[ ]比较数字

[root@wangjingjing 0131]# [ 2 \> 3 ] && echo true || echo false

false

[root@wangjingjing 0131]# [ 2 -gt 3 ] && echo true || echo false

false

[[ ]]比较字符串和(()) 比较字符串

[root@wangjingjing 0131]# [[ 2 < 3 ]] && echo true || echo false

true

[root@wangjingjing 0131]# (( 2 < 3 )) && echo true || echo false

true

(( ))只能比较整数数字

[root@wangjingjing 0131]# (( abc < abd )) && echo true || echo false

false

[root@wangjingjing 0131]# [[ abc < abd ]] && echo true || echo false

true

六,逻辑操作符 (在里面使用)

在[]中使用的逻辑操作符   用字母

在test、[[]]和(())中使用的逻辑操作符    用符号

1, [  ]

与:

[root@wangjingjing 0131]# [ 2 -lt 3 -a 3 -lt 4 ] && echo true || echo false

true

[root@wangjingjing 0131]# [ 2 -lt 3 -a 3 -gt 4 ] && echo true || echo false

false

或:

[root@wangjingjing 0131]# [ 2 -lt 3 -o 3 -gt 4 ] && echo true || echo false

true

[root@wangjingjing 0131]# [ 2 -gt 3 -o 3 -gt 4 ] && echo true || echo false

false

非:

[root@wangjingjing 0131]# [ ! 3 -gt 4 ] && echo true || echo false

true

2,test

[root@wangjingjing 0131]# test 3 -gt 2 && test 7 -gt 6 && echo true || echo false

true

3,[[   ]]

[root@wangjingjing 0131]# [[ 3 > 2 && 1 > 3 ]] && echo true || echo false

false

[root@wangjingjing 0131]# [[ 3 > 2 && 1 < 3 ]] && echo true || echo false

true

[root@wangjingjing 0131]# [[ 3 > 2 || 1 > 3 ]] && echo true || echo false

true

[root@wangjingjing 0131]# [[ ! 1 > 3 ]] && echo true || echo false

true

4,((  ))比较整数

[root@wangjingjing 0131]# ((3>4 &&  1<2)) && echo true || echo false

false

[root@wangjingjing 0131]# ((3<4 ||  1<2)) && echo true || echo false

true

[root@wangjingjing 0131]# ((! 3>4)) && echo true || echo false

false

[root@wangjingjing 0131]# ((! 4>3)) && echo true || echo false

false

实验1

通过read传入一个数字,如果传入的数字等于1,就打印1;如果等于2,就打印2,如果不等于1 也不等于2,就提示输入不对,然后退出程序。

1,打开文件num_test.sh 编辑保存退出

[root@wangjingjing 0131]# vim num_test.sh

#输入

read -p "please input a number: " num

#输入的数字如果等于1,打印1,退出状态码为1

[ $num -eq 1 ] && {

    echo 1

    exit 1

}

#输入的数字如果等于2,打印2,退出状态码为2

[[ $num == 2 ]] && {

    echo 2

    exit 2

}

#输入的数字如果不等于1,也不等于2,输出input incorrect,退出状态码为3

test $num -ne 1 && test $num -ne 2  && {

    echo "input incorrect"

    exit 3

}

2,重新加载文件,验证

[root@wangjingjing 0131]# bash num_test.sh

please input a number: 1

1

[root@wangjingjing 0131]# bash num_test.sh

please input a number: 2

2

[root@wangjingjing 0131]# bash num_test.sh

please input a number: 3

input incorrect

[root@wangjingjing 0131]# bash num_test.sh

please input a number: 66

input incorrect

实验2

通过read读入两个整数,并比较他们的大小

1,打开文件num_test2.sh编辑保存并退出

[root@wangjingjing 0131]# vim num_test2.sh

#判定输入的正确性:num1 和 num2 不为空;num1 和 num2 必须是整数

test -z "$num1" || test -z "$num2" && {

    echo "please input two number"

    exit 111

}

#判断是否是数字:expr

expr $num1 + 1 &> /dev/null

exit_code=$?

[ $exit_code -ne 0 ] && {

    echo "num1 is not integer"

    exit 222

}

expr $num2 + 1 &> /dev/null

exit_code=$?

[[ $exit_code != 0 ]] && {

    echo "num2 is not integer"

    exit 222

}

#如果num1<num2,打印num1<num2,退出状态码为0

[ $num1 -lt $num2 ] && {

    echo "$num1 < $num2 "

    exit 0

}

#如果num1>num2,打印num1>num2,退出状态码为0

test $num1 -gt $num2 && {

    echo "$num1 > $num2"

    exit 0

}

#如果num1=num2,打印num1=num2,退出状态码为0

[[ $num1 = $num2 ]] && {

    echo "$num1 == $num2"

    exit 0

}

2,重新加载文件,测试

(1)判定输入的正确性:num1 num2 不为空;num1 num2 必须是整数

[root@wangjingjing 0131]# bash num_test2.sh

please inpur two numbers: 1

please input two number

[root@wangjingjing 0131]# bash num_test2.sh

please inpur two numbers:  2

please input two number

[root@wangjingjing 0131]# bash num_test2.sh

please inpur two numbers: a

please input two number

(2)判断是否是数字:expr

[root@wangjingjing 0131]# bash num_test2.sh

please inpur two numbers: 1 c

num2 is not integer

[root@wangjingjing 0131]# bash num_test2.sh

please inpur two numbers: c 2

num1 is not integer

(3)如果num1<num2,打印num1<num2,退出状态码为0

[root@wangjingjing 0131]# bash num_test2.sh

please inpur two numbers: 1 5

1 < 5

[root@wangjingjing 0131]# echo $?

0

(4)如果num1>num2,打印num1>num2,退出状态码为0

[root@wangjingjing 0131]# bash num_test2.sh

please inpur two numbers: 7 3

7 > 3

[root@wangjingjing 0131]# echo $?

0

(5)如果num1=num2,打印num1=num2,退出状态码为0

[root@wangjingjing 0131]# bash num_test2.sh

please inpur two numbers: 6 6

6 == 6

[root@wangjingjing 0131]# echo $?

0

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/190701.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

重学MySQL基础(一)

文章目录重学MySQL基础&#xff08;一&#xff09;MySQL 连接管理MySQL字符编码InnoDB 记录存储结构InnoDB 表的主键生成策略&#xff1a;InnoDB 数据页结构页目录页的效验和索引事务报错记录在MySQL中创建函数时出现这种错误恶补SQL语句SQL中的条件语句SQL中的字符串函数SQL中…

python调用go语言踩坑记录

目录 基本操作 1 在go文件中加注释&#xff0c;设置为导出方法,导出C依赖 2 导出so文件&#xff08;mac或者linux下只需要so&#xff09; 3 进行调用 报错记录 踩坑1 关于结构体 2 cannot use (_Cfunc_CString)("12345") (value of type *_Ctype_char) as ty…

spring中事务失效场景

文章目录spring中事务失效场景一、权限访问问题二、方法用final修饰三、无事务嵌套有事务的方法四、没有被spring管理五、设计的表不支持事务六、没有开启事务七、错误的事务传播八、自己捕获了异常九、手动抛出别的异常十、自定义回滚异常spring中事务失效场景 一、权限访问问…

软件研发管理经验总结 - 事务管理

软件研发管理经验总结 - 事务管理 相关系列文章 软件产品研发管理经验总结-管理细分 软件研发管理经验总结 - 事务管理 目录软件研发管理经验总结 - 事务管理一、概述二、事务管理过程1、制定开发计划2、启动会议3、阅读前一天的日报4、例会/早会5、调整计划6、协调资源7、日报…

LeetCode——2325. 解密消息

一、题目 给你字符串 key 和 message &#xff0c;分别表示一个加密密钥和一段加密消息。解密 message 的步骤如下&#xff1a; 使用 key 中 26 个英文小写字母第一次出现的顺序作为替换表中的字母 顺序 。 将替换表与普通英文字母表对齐&#xff0c;形成对照表。 按照对照表…

vue全家桶(三)前端路由

vue全家桶&#xff08;三&#xff09;前端路由1.路由的概念1.1路由1.2vue Router2.vue-router的基本使用步骤2.1基本使用步骤2.2路由重定向3.vue-router的嵌套路由用法3.1嵌套路由的用法4.vue-router动态路由匹配用法5.vue-router命名路由用法6.vue-router编程式导航用法6.1 页…

06 Sentinel控制台规则配置讲解 (2)

1、实时监控 监控接口的通过的QPS和拒绝的QPS 2、簇点链路 用来显示微服务的所监控的API 3、流控规则 流量控制&#xff08;flow control&#xff09;&#xff0c;其原理是监控应用流量的 QPS 或并发线程数等指标&#xff0c;当达到指定的阈值时对流量进行控制&#xff0c;以…

源于《C陷阱与缺陷》----研究程序死循环问题

本题来源于《C陷阱与缺陷》这本书&#xff0c;从本质上讲解程序死循环的原因&#xff0c;关键在于栈的空间使用方式。研究程序死循环的原因死循环的原因是什么呢&#xff1f;解决方法总结研究程序死循环的原因 题目1&#xff1a; 在VS2019 X86环境下测试&#xff1a; int mai…

Linux服务器怎么设置iptables防火墙?

当今&#xff0c;为避免香港服务器及其端口受到恶意入侵&#xff0c;保障香港服务器的安全&#xff0c;配置Linux服务器防火墙的重要性不言而喻。恒创科技将向您介绍如何在 CentOS、Ubuntu 和 Debian 服务器中安装和配置 iptables防火墙。iptables是一个简单的防火墙&#xff0…

测试工程师必备技能之编写测试用例

1. 必要性 &#x1f449; 指导测试工作&#xff0c;用例通过率是评估质量的基准 &#x1f449; 完善测试人员测试的整体思路&#xff0c;不出现漏测 ❗️ 避免背锅&#xff0c;线上出了问题防止开发甩锅给测试 2. 模板 企业中用例往往是每个人负责不同模块&#xff0c;再根据…

Verilog实现超前进位加法器

在CPU等对性能要求较高的电路中&#xff0c;一般都会采用超前进位加法器&#xff0c;因为超前进位加法器的延时相对来说比较小。下面讲述超前进位加法器的原理&#xff1a; 我们知道&#xff0c;一个三输入&#xff0c;二输出的全加器&#xff0c;其逻辑关系为 SA⊕B⊕CinSA\op…

每日一练9——另类加法走方格的方案数

文章目录另类加法思路&#xff1a;代码&#xff1a;走方格的方案数思路&#xff1a;代码&#xff1a;另类加法 题目链接 思路&#xff1a; 本题可以通过位运算实现&#xff0c;具体实现如下&#xff1a; 两个数求和&#xff0c;其实就是 求和后当前位的数据两个数求和的进位…

Ceres Solver解算已知函数模型参数

一、介绍Ceres solver 是谷歌开发的一款用于非线性优化的库&#xff0c;在已知一个函数表达式&#xff0c;以及一组观测到的值&#xff0c;利用最小二乘是可以解算得到相关参数。下面举例使用ceres solver解算直线函数以及曲线函数参数。其过程包括三个步骤&#xff1a;&#x…

图为技术:专注核心技术 引领行业发展

图为技术T-3D引擎经过多年的技术积累与原始创新&#xff0c;作为国内首批基于分布式技术进行三维图形底层渲染的开拓者&#xff0c;不仅在渲染架构上优势明显&#xff0c;同时其所具备数据管理能力也十分卓越。分布式架构采用独创的(Multi Compute Server To Web Render)架构方…

Java多线程之CAS中的ABA问题与JUC的常见类

文章目录一. CAS指令与ABA问题1. 解析CAS2. 基于CAS实现的原子类3. 基于CAS实现自旋锁4. ABA问题二. JUC中的常见类1. Callable接口2. ReentrantLock类(可重入锁)3. Semaphore类(信号量)4. CountDownLatch同步工具类一. CAS指令与ABA问题 1. 解析CAS CAS即compare and awap, …

Cesium 坐标系的转换,空间数据加载 粒子系统加载

在vue中引入Cesium.js官网下载好的Cesium文件放入vue项目中index.html中引入,在js文件即可智能提示&#xff0c;或者下载依赖包也可<script src"./Cesium/Cesium.js"></script><link rel"stylesheet" href"./Cesium/Widgets/widgets.c…

若依配置教程(六)Excel导入功能实现

若依官网导入实现流程 文章目录一、前端index.vue中1.在所需模块的index.vue中的< script >< /script >中增加如下代码&#xff1a;2.在< template >< /template >中添加导入按钮事件&#xff1a;3.添加导入前端代码&#xff1a;二、在模块文件夹(ruoy…

数学建模学习笔记(17)灰色预测模型和神经网络

文章目录灰色预测模型相关基本概念GM(1,1)模型的使用步骤GM(1,1)模型的拓展模型GM(1,1)模型的注意事项BP神经网络预测模型的注意事项灰色预测模型 相关基本概念 系统的分类&#xff1a; 白色系统&#xff1a;系统的信息是完全明确的。灰色系统&#xff1a;系统的部分信息已知…

readelf指令使用

一、指令说明readelf命令&#xff0c;一般用于查看ELF格式的文件信息&#xff0c;常见的文件如在Linux上的可执行文件&#xff0c;动态库(*.so)或者静态库(*.a) 等包含ELF格式的文件。以下命令的使用是基于android编译出来的so文件上面去运行。readelf常用命令语法&#xff1a;…

视网膜电图特征可以检测成人的抑郁状态和治疗反应:一种机器学习方法

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 视网膜电图特征可以检测成人的抑郁状态和治疗反应&#xff1a;一种机器学习方法摘要1. 引言2. 方法和材料2.1 人口与伦理声明2.2 入选标准、临床和生物学评估2.3 实验方案2.4…