Shell编程之条件判断语句

Shell编程之条件判断语句

一、条件判断

Shell环境根据命令执行后的返回状态值(echo $?)来判断是否执行成功,当返回值为0表示成功或正确,返回值为非0值表示失败或异常。(补充:Linux判断依据在别的编程语言中是反过来的,如java假为0,真为1)

1、test命令

有两种方式

  • test 条件表达式
  • [ 条件表达式 ] [ ] 相当于test 中括号之间需要有空格

案例:0表示成立 其他值表示不成立

[root@VM-20-11-centos shell]# test 1=1
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]# [ 1=1 ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#

2、文件测试

  • test 选项 文件/目录路径 test 选项 $变量
  • [ 选项 文件/目录路径 ] [ 选项 $变量 ]

常用的操作符号有

-e 测试目录或文件是否存在(Exist)
-d 测试是否为目录(Directory)
-f 测试是否为文件(File)
-r 测试当前用户是否有权限读取(Read)
-w 测试当前用户是否有权限写入(Write)
-x 测试是否设置有可执行(Excute)权限。
-L 测试是否为符号链接

案例:

# 返回值为1代表不存在
[root@VM-20-11-centos shell]# [ -e wss ]
[root@VM-20-11-centos shell]# echo $?
1
[root@VM-20-11-centos shell]#
# 返回值为1代表不是目录
[root@VM-20-11-centos shell]# [ -d wss ]
[root@VM-20-11-centos shell]# echo $?
1
[root@VM-20-11-centos shell]#
# 返回值为0代表是文件
[root@VM-20-11-centos shell]# ls -alh
total 28K
drwxrwxrwx 2 root root 4.0K Jul  8 10:10 .
drwxr-xr-x 5 root root 4.0K Jul  5 10:46 ..
-rw-r--r-x 1 root root  341 Jul  8 09:47 auto_var.sh
-rw-r--r-x 1 root root   54 Jul  5 17:44 first_shell.sh
-rw-r--r-x 1 root root  146 Jul  8 10:03 test_dir.sh
-rw-r--r-x 1 root root   85 Jul  8 09:59 test_num.sh
-rw-r--r-x 1 root root  200 Jul  8 10:11 test_scores.sh
[root@VM-20-11-centos shell]# [ -f auto_var.sh ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#

3、整数值比较

  • 格式:[ 整数变量1 操作符 整数变量2 ]

常用的操作符

-eq 等于
-ne 不等于
-gt 大于
-lt 小于
-le 小于等于
-ge 大于等于

案例

[root@VM-20-11-centos shell]# [ 1 -le 1 ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#

4、字符串判断

格式

  • test "字符串1" == "字符串2"
  • [ "字符串1" == "字符串2" ]

案例:

[[ "字符串1" =~ "字符串2 ]] # 判断字符串1是否包含字符串2

test -z "字符串" test -z "$变量" [ -z "$变量" ] #判断字符串或变量是否为空

test -n "字符串" test -n "$变量" [ -n "$变量" ] #判断字符串或变量是否有字符串


[root@VM-20-11-centos shell]# [ "abc" == "123" ]
[root@VM-20-11-centos shell]# echo $?
1[root@VM-20-11-centos shell]# [[ "abcdfre" =~ "bc" ]]
[root@VM-20-11-centos shell]# echo $?
0[root@VM-20-11-centos shell]# [ -z "a" ]
[root@VM-20-11-centos shell]# echo $?
1
[root@VM-20-11-centos shell]# [ -z "" ]
[root@VM-20-11-centos shell]# echo $?
0[root@VM-20-11-centos shell]# [ -n ""]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]# [ -n null ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#

5、逻辑测试

  • 格式1:[ 表达式1 ] 操作符 [ 表达式2 ]
  • 格式2:命令1 操作符 命令2

常用操作符 -a或者&& 而且的意思

             -o或者 ll  或者的意思!: 代表取反[ 表达式1 ] && [ 表达式2 ] ;[ 表达式1 -a 表达式2 ] ;[[ 表达式1 && 表达式2 ]]
[root@VM-20-11-centos shell]# [ 1=1 ] && [ 2=2 ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]# [ 1=1 -a 2=2 ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]# [[ 1=1 && 2=2 ]]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#
[root@VM-20-11-centos shell]# [[ 1=1 || 2=3 ]]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]# [ 1=1 -o "abc"=="bdc" ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#
# 取反
[root@VM-20-11-centos shell]# [ ! 12=12 ]
[root@VM-20-11-centos shell]# echo $?
1
[root@VM-20-11-centos shell]# [ 12=12 ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#

案例2

#!/bin/bash
str="yingtan"
if [[ $str == "yingtan" || $str == "鹰潭" ]];then echo "鹰潭"
elseecho "不是鹰潭"
fi

执行结果

[root@VM-20-11-centos shell]# sh test_str.sh
鹰潭
[root@VM-20-11-centos shell]#

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Ubuntu 2204 安装使用 mariadb

1. 查看mariadb版本,实际上使用的还是mysqld命令: mysqld --version 2. 3. 4. 5. 6. 7.

Docker安装命令

事先声明-对于命令直接粘进去执行就可以Docker 分为 CE 和 EE 两大版本。CE 即社区版(免费,支持周期 7 个月),EE 即企业版,强调安全,付费使用,支持周期 24 个月。 Docker CE 分为 stable test 和 nightly 三个更新频道。 最重要一点:Docker CE 支持 64 位版本 CentOS 7…

elemenet 级联

两种数据格式 注释掉的是一种格式,未注释的是另一种格式。<template><div><!-- <select v-model="selectedProvince" @change="provinceChanged"><option v-for="province in provinces" :value="province.code&q…

树莓派4B-PCA9685驱动舵机

直接用树莓派的引脚输出PWM控制舵机,舵机是会出现抖动的。就算代码进行一定的时延迟优化还是会有影响的。现在我们可以使用PCA9685这个模块去驱动舵机,做到高精度控制舵机。前言 不知道你们有没有遇到过这么一种情况,直接用树莓派的引脚输出PWM控制舵机,舵机是会出现抖动的…

docker-compose vs docker-stack

docker-compose vs docker-stack 都是docker两个容器编排工具,docker-compose是属于第三方容器编排工具需要单独安装,docker-stack是docker内置容器编排工具。 docker-compose一般配合K8S使用,目前要容器管理方面K8S有着比较明显的优势,所以docker-compose 目前比较流行。 …

DDP:微软提出动态detection head选择,适配计算资源有限场景 | CVPR 2022

DPP能够对目标检测proposal进行非统一处理,根据proposal选择不同复杂度的算子,加速整体推理过程。从实验结果来看,效果非常不错 来源:晓飞的算法工程笔记 公众号论文: Should All Proposals be Treated Equally in Object Detection?论文地址:https://arxiv.org/abs/2207…

ENVIFormat开源样本库使用教程

前段时间分享了两个开源样本库:GID-ENVIFormat和Five-Billion-Pixels-ENVIFormat样本库。这两个样本库均包含大量影像底图和高质量的样本数据。GID-ENVIFormat样本库包含5类别和15类别样本数据,Five-Billion-Pixels-ENVIFormat包含24类别样本数据。有关样本库数据的获取可参考…

新架构下服务建模,关键在这6步!

经纬恒润基于SystemWeaver平台,按照SOA建模理念为客户提供了新一代基于SOA的企业级电子电气系统协同设计解决方案,可以有效支持服务和信号的混合架构建模。 随着AUTOSAR、SOA、以太网通讯等新技术、新理念的成熟化,面向软件、硬件、网络、电气等多领域的电子电气系统经…

Bond——大数据时代的数据交换和存储格式

设想我们在一家很大的互联网公司做IT方面的规划、开发和维护,有以下这样的应用场景:公司里有若干个不同的开发团队,开发语言有Java、.net、Python、C++....十来种,还有很多外包团队对项目进行开发,大中小系统已经多的数不过来;并且各个团队、系统间都需要进行海量数据的交…

订阅arXiv每日最新论文

邮箱订阅论文 arXiv 参考如何利用邮箱订阅 arxiv,接收每日最新的 arxiv 文章 订阅 订阅它的论文,需要用自己的邮箱像 arXiv 发送邮件。To: cs@arxiv.orgSubject: subscribe Your Nameadd Artificial Intelligencedel Systems and Control我们需要从arxiv.org上查找自己对应的方…

ali140滑块

ali140滑块记得加如我们的学习群: 961566389获取更多资讯。 ali 140滑块采用补环境的方式进行逆向,需要的文件主要为这两个,其中collina.js是最主要的环境校验和参数加密的逻辑,这两个文件放本地进行补环境。然后挂上代理,先保证能够正常运行代码,补了些许后能够正常运行…

【日记】我就是世界上最幸福的人!(1124 字)

正文今天想写的内容有点多,就不写在纸上了。首先,最高兴的,还是我们的《艾尔登法环》有了进展。我和兄长终于通过了 “火山官邸:地底拷问所”。我真是不知道,我和他在这个地方被那两个掳人少女人拷问了多少次了。不仅如此,拉塔恩也打过了,去了亚坛高原,反正进展很大。周…