linux shell 入门学习笔记15 shell 条件测试

news2024/9/30 9:19:25

概念

shell的条件测试目的是得出真和假。

shell 提供的条件测试语法
test 命令
[] 中括号命令

语法*:
条件测试

test条件测试

test命令用来评估一个表达式,他的结果是真,还是假,如果条件为真,那么命令执行状态结果就为0,否则就是不为0,通过$?取值。

test命令参数
============检测给定文件名是否存在
-e 判断该文件是否存在,(普通文件,目录),存在就为真,否则为假
============检测给定文件名的类型
-f 判断该文件名是否为文件
-d 判断该文件名是否为目录
-b 判断该文件名是否为块设备
-c 判断该文件名是否为字符设备
-s 判断该文件名是否为socket
-p 判断该文件名是否为管道FIFO
-L 判断该文件名是否为连接
============检测文件的权限
-r 判断该文件名是否可读
-w 判断该文件名是否可写
-x 判断该文件名是否可运行
-u 判断该文件名是否有SUID属性
-g 判断该文件名是否有SGID属性
-k 判断该文件名是否有Sticky bit属性
-s 判断该文件名是否为空白文件
============文件比较 如test file1 -nt file2
-nt (newer than)判断file1是否比file2新
-ot(older than)判断file1是否比file2旧
-ef 判断file1与file2是否为同一个文件,可用在判断hard link的判定上,主要意义在判定两个文件是否均指向同一个inode
============整数之间判定
-eq 两数值相等
-ne 两数值不等
-gt n1大于n2 (greater than)
-lt n1小于n2(less than)
-ge n1大于等于n2 (greater than or equal)
-le n1小于等于n2(less than or equal)
============判定字符串数据
-z 判断字符串是否为0,若字符串为空,则为true
-n 判断字符串是否为非0,若字符串为非空,则为true
= 判断str1与str2是否相等,相等则为true
!= 判断str1与str2是否不想等,不相等则为true
============多重条件判断
-a (and)两状态同时成立,则返回true
-o (or)两状态任意一个成立,则返回true
! 反向状态

test命令实践

-e演示

xiao123@xiao123:~/Downloads/shscripts$ test -e del_data.sh
xiao123@xiao123:~/Downloads/shscripts$ echo $?
0
xiao123@xiao123:~/Downloads/shscripts$ test -e del_data.sh12
xiao123@xiao123:~/Downloads/shscripts$ echo $?
1
xiao123@xiao123:~/Downloads/shscripts$
xiao123@xiao123:~/Downloads/shscripts$ test -e del_data.sh && echo 文件已存在
文件已存在
xiao123@xiao123:~/Downloads/shscripts$ test -e del_data.sh12 && echo 文件已存在
xiao123@xiao123:~/Downloads/shscripts$ test -e del_data.sh12 || echo 文件不存在
文件不存在
xiao123@xiao123:~/Downloads/shscripts$
xiao123@xiao123:~/Downloads/shscripts$ test -e hello && echo 该文件/目录已存在,不再执行创建动作 || mkdir hello
xiao123@xiao123:~/Downloads/shscripts$ test -e hello && echo 该文件/目录已存在,不再执行创建动作 || mkdir hello
该文件/目录已存在,不再执行创建动作
xiao123@xiao123:~/Downloads/shscripts$

-f演示

xiao123@xiao123:~/Downloads/shscripts$ ls
calculation.sh           chaochao_5_finished.png  index.html.11  index.html.2   index.html.28  index.html.6
chaochao_1_finished.png  chaochao_5.jpg           index.html.12  index.html.20  index.html.29  index.html.7
chaochao_1.jpg           del_data.sh              index.html.13  index.html.21  index.html.3   index.html.8
chaochao_2_finished.png  expr_test1.sh            index.html.14  index.html.22  index.html.30  index.html.9
chaochao_2.jpg           expr_test.sh             index.html.15  index.html.23  index.html.31  let_test.sh
chaochao_3_finished.png  hello                    index.html.16  index.html.24  index.html.32
chaochao_3.jpg           index.html               index.html.17  index.html.25  index.html.33
chaochao_4_finished.png  index.html.1             index.html.18  index.html.26  index.html.4
chaochao_4.jpg           index.html.10            index.html.19  index.html.27  index.html.5
xiao123@xiao123:~/Downloads/shscripts$ test -f hello && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$

-d演示

xiao123@xiao123:~/Downloads/shscripts$ ls
calculation.sh           chaochao_5_finished.png  index.html.11  index.html.2   index.html.28  index.html.6
chaochao_1_finished.png  chaochao_5.jpg           index.html.12  index.html.20  index.html.29  index.html.7
chaochao_1.jpg           del_data.sh              index.html.13  index.html.21  index.html.3   index.html.8
chaochao_2_finished.png  expr_test1.sh            index.html.14  index.html.22  index.html.30  index.html.9
chaochao_2.jpg           expr_test.sh             index.html.15  index.html.23  index.html.31  let_test.sh
chaochao_3_finished.png  hello                    index.html.16  index.html.24  index.html.32
chaochao_3.jpg           index.html               index.html.17  index.html.25  index.html.33
chaochao_4_finished.png  index.html.1             index.html.18  index.html.26  index.html.4
chaochao_4.jpg           index.html.10            index.html.19  index.html.27  index.html.5
xiao123@xiao123:~/Downloads/shscripts$ test -d hello && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$

-z/-n演示

xiao123@xiao123:~/Downloads/shscripts$ test -d hello && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$ test -z "" && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$ test -z " " && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ test -n "" && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ test -n " " && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$

中括号条件测试

脚本中经常进行条件测试,用的最多的都是中括号。
test和[]的作用是一样的。
注意点:中括号前后都要有空格[ ]

[ -n “${filename}” ]
注意在条件测试中,使用变量必须要添加双引号""

xiao123@xiao123:~/Downloads/shscripts$ file=del_data.sh
xiao123@xiao123:~/Downloads/shscripts$ [ -f "${file}" ] && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$ file=del_data.sh12
xiao123@xiao123:~/Downloads/shscripts$ [ -f "${file}" ] && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$

双中括号条件测试

双中括号增加了正则表达式的支持,其他与中括号相同。

变量测试

把字符串写入变量中
对变量测试,必须要加双引号

xiao123@xiao123:~/Downloads/shscripts$ file1="鸡你太美.jpg"
xiao123@xiao123:~/Downloads/shscripts$ [[ -f "${file1}" ]] && echo "我是两年半的练习生,鸡你太美" || echo "我是个好人"
我是个好人
xiao123@xiao123:~/Downloads/shscripts$ [[ -f "${file1}" ]] && echo "我是两年半的练习生,鸡你太美" || echo "我是个好人"
我是个好人
xiao123@xiao123:~/Downloads/shscripts$ touch "鸡你太美.jpg"
xiao123@xiao123:~/Downloads/shscripts$ [[ -f "${file1}" ]] && echo "我是两年半的练习生,鸡你太美" || echo "我是个好人"
我是两年半的练习生,鸡你太美
xiao123@xiao123:~/Downloads/shscripts$
字符串测试

比较两个字符串变量的值,是否相等,不等的情况。

= 判断是否相等
!= 判断不相等
! 取结果的反义,颠倒黑白

注意:
对于字符串变量的比较一定要给变量添加双引号
使用等于号判断,左右两边必须有空格

演示

xiao123@xiao123:~/Downloads/shscripts$ [ ! -f "糖果超甜组合.txt" ] && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$ [ -f "糖果超甜组合.txt" ] && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$
数值比较测试

操作方式
数值计算

  1. 在中括号以及test中数值比较的用法

在中括号中,使用数学比较符号,请添加转义符号

xiao123@xiao123:~/Downloads/shscripts$ [ 2 > 1 ] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [ 1 > 2 ] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [ 1 \> 2 ] && echo yes || echo no
no
xiao123@xiao123:~/Downloads/shscripts$
xiao123@xiao123:~/Downloads/shscripts$ [ 2 \= 2 ] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [ 2 != 2 ] && echo yes || echo no
no   #不等于可以不用添加转义
xiao123@xiao123:~/Downloads/shscripts$ [ 3 != 2 ] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [ 3 \!= 2 ] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [ 3 \!\= 2 ] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [ 3 \!\= 3 ] && echo yes || echo no
no
xiao123@xiao123:~/Downloads/shscripts$
  1. 双中括号
    对中括号的补充,双中括号还支持正则处理。
    在双中括号中不要转义字符。
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 > 6 ]] && echo yes || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 < 6 ]] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 = 6 ]] && echo yes || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 != 6 ]] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 -ge 6 ]] && echo yes || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 -le 6 ]] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 -gt 6 ]] && echo yes || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 -lt 6 ]] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$
逻辑操作符号测试

&& -a 与运算, 两边都为真,结果才为真
|| -o 或运算,两边有一个为真,结果为真
逻辑操作

中括号逻辑运算比较

xiao123@xiao123:~/Downloads/shscripts$ file1=/etc/init.d/rsync
xiao123@xiao123:~/Downloads/shscripts$ file2=/etc/hostname
xiao123@xiao123:~/Downloads/shscripts$ [ -f "${file1}" -a -f "${file2}" ] && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$ file1=/tmp/sqqqq
xiao123@xiao123:~/Downloads/shscripts$ [ -f "${file1}" -a -f "${file2}" ] && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ [ -f "${file1}" -o -f "${file2}" ] && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$

双中括号运算比较

xiao123@xiao123:~/Downloads/shscripts$ str1=""
xiao123@xiao123:~/Downloads/shscripts$ str2="yuyu"
xiao123@xiao123:~/Downloads/shscripts$ [[ -n "${str1}" && -n "${str2}" ]] && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ [[ -n "${str1}" || -n "${str2}" ]] && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$

脚本开发

要求:接收用户输入,判断它是否等于某个数字。

xiao123@xiao123:~/Downloads/shscripts$ bash ./test_input.sh
Please input a char: 3
脚本出错,必须输入1和2
xiao123@xiao123:~/Downloads/shscripts$ bash ./test_input.sh
Please input a char: 1
1
xiao123@xiao123:~/Downloads/shscripts$ bash ./test_input.sh
Please input a char: 2
2
xiao123@xiao123:~/Downloads/shscripts$ cat ./test_input.sh
#! /bin/bash

read -p "Please input a char: " var1

[ "${var1}" -eq "1" ] && {
        echo ${var1}
        exit 0
}

[[ "${var1}" = "2" ]] && {
        echo ${var1}
        exit 0
}

[ "${var1}" != 2 -a "${var1}" != 1 ] && {
        echo "脚本出错,必须输入1和2"
        exit 1
}
xiao123@xiao123:~/Downloads/shscripts$

要求:安装lnmp/lamp脚本开发。

xiao123@xiao123:~/Downloads/shscripts$ mkdir test
xiao123@xiao123:~/Downloads/shscripts$ echo "echo LAMP is installed" > ./test/lamp.sh
xiao123@xiao123:~/Downloads/shscripts$ echo "echo LNMP is installed" > ./test/lnmp.sh
xiao123@xiao123:~/Downloads/shscripts$ chmod +x ./test/lamp.sh
xiao123@xiao123:~/Downloads/shscripts$ chmod +x ./test/lnmp.sh
xiao123@xiao123:~/Downloads/shscripts$

源码

xiao123@xiao123:~/Downloads/shscripts$ cat ./test_install.sh
#! /bin/bash

path=./test

[ ! -d  "${path}" ] && mkdir ${path} -p

cat << END
        1. [install lamp]
        2. [install lnmp]
        3. [exit]
        please input the num you want:
END

read num

expr ${num} + 1 &>/dev/null

[ $? -ne 0 ] && {
        echo "input number must be {1|2|3}"
        exit 1
}

[ ${num} -eq 1 ] && {

        echo "Strating installing lamp.....waiting..."
        sleep 2

        [ -x ${path}/lamp.sh ] || {
                echo "The file does not exist or can't be execut."
                exit 1
        }

        ${path}/lamp.sh
        exit $?
}

[ ${num} -eq 2 ] && {

        echo "Strating installing lnmp.....waiting..."
        sleep 2

        [ -x ${path}/lamp.sh ] || {
                echo "The file does not exist or can't be execut."
                exit 1
        }

        ${path}/lnmp.sh
        exit $?
}

[ ${num} -eq 3 ] && {
        echo "byebye"
        exit 3
}

[[ ! ${num} =~ [1-3] ]] && {
        echo "The number input must be {1|2|3}"
        exit 4
}
xiao123@xiao123:~/Downloads/shscripts$

运行结果

xiao123@xiao123:~/Downloads/shscripts$ bash ./test_install.sh
        1. [install lamp]
        2. [install lnmp]
        3. [exit]
        please input the num you want:
e
input number must be {1|2|3}
xiao123@xiao123:~/Downloads/shscripts$ bash ./test_install.sh
        1. [install lamp]
        2. [install lnmp]
        3. [exit]
        please input the num you want:
3
byebye
xiao123@xiao123:~/Downloads/shscripts$ bash ./test_install.sh
        1. [install lamp]
        2. [install lnmp]
        3. [exit]
        please input the num you want:
1
Strating installing lamp.....waiting...
LAMP is installed
xiao123@xiao123:~/Downloads/shscripts$ bash ./test_install.sh
        1. [install lamp]
        2. [install lnmp]
        3. [exit]
        please input the num you want:
2
Strating installing lnmp.....waiting...
LNMP is installed
xiao123@xiao123:~/Downloads/shscripts$

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

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

相关文章

【蓝桥杯集训·周赛】AcWing 第92场周赛

文章目录第一题 AcWing 4864. 多边形一、题目1、原题链接2、题目描述二、解题报告1、思路分析2、时间复杂度3、代码详解第二题 AcWing 4865. 有效类型一、题目1、原题链接2、题目描述二、解题报告1、思路分析2、时间复杂度3、代码详解第三题 AcWing 4866. 最大数量一、题目1、原…

Spring之丐版IOC实现

文章目录IOC控制反转依赖注入Bean的自动装配方式丐版IOC实现BeanDefinition.javaResourceLoader.javaBeanRegister.javaBean和DI的注解BeanFactory.javaApplicationContext测试&#xff0c;实现在这里插入图片描述大家好&#xff0c;我是Leo。Spring核心中依赖注入和IOC容器是非…

JWT令牌

1.普通令牌的问题 客户端申请到令牌&#xff0c;接下来客户端携带令牌去访问资源&#xff0c;到资源服务器将会校验令牌的合法性。 从第4步开始说明&#xff1a; 1、客户端携带令牌访问资源服务获取资源。 2、资源服务远程请求认证服务校验令牌的合法性 3、如果令牌合…

QML Image and Text(图像和文字)

Image&#xff08;图片&#xff09; 图像类型显示图像。 格式&#xff1a; Image {source: "资源地址" } source&#xff1a;指定资源的地址 自动检测文件拓展名&#xff1a;source中的URL 指示不存在的本地文件或资源&#xff0c;则 Image 元素会尝试自动检测文件…

MVI 架构更佳实践:支持 LiveData 属性监听

前言MVI架构为了解决MVVM在逻辑复杂时需要写多个LiveData(可变不可变)的问题,使用ViewState对State集中管理&#xff0c;只需要订阅一个 ViewState 便可获取页面的所有状态通过集中管理ViewState&#xff0c;只需对外暴露一个LiveData&#xff0c;解决了MVVM模式下LiveData膨胀…

Linux_vim编辑器入门级详细教程

前言&#xff08;1&#xff09;vim编辑器其实本质上就是对文本进行编辑&#xff0c;比如在.c文件中改写程序&#xff0c;在.txt文件写笔记什么的。一般来说&#xff0c;我们可以在windows上对文本进行编译&#xff0c;然后上传给Linux。但是有时候我们可能只是对文本进行简单的…

MySQL运维

目录 1、日志 1、错误日志 2、二进制日志 3、查询日志 4、慢查询日志 2、主从复制 搭建 1、主库配置 2、从库配置 3、分库分表 1、简介 ​编辑 1、垂直拆分 2、水平拆分 3、实现技术 2、MyCat 3、MyCat使用和配置 配置 4、MyCat分片 1、垂直拆分 2、水平拆分…

线材-电子线载流能力

今天来讲的是关于电子线的一个小知识&#xff0c;可能只做板子的工程师遇到此方面的问题会比较少&#xff0c;做整机的工程师则必然会遇到此方面问题&#xff0c;那就是线材问题。 下面主要说下电子线的过电流能力。&#xff08;文末有工具下载&#xff09;电子线&#xff08;h…

vue3+rust个人博客建站日记2-确定需求

反思 有人说过我们正在临近代码的终结点。很快&#xff0c;代码就会自动产生出来&#xff0c;不需要再人工编写。程序员完全没用了&#xff0c;因为商务人士可以从规约直接生成程序。 扯淡&#xff01;我们永远抛不掉代码&#xff0c;因为代码呈现了需求的细节。在某些层面上&a…

CTFer成长之路之Python中的安全问题

Python中的安全问题CTF 1.Python里的SSRF 题目提示 尝试访问到容器内部的 8000 端口和 url path /api/internal/secret 即可获取 flag 访问url&#xff1a; http://f5704bb3-5869-4ecb-9bdc-58b022589224.node3.buuoj.cn/ 回显如下&#xff1a; 通过提示构造payload&…

Pytorch复习笔记--Conv2d和Linear的参数量和显存占用量比较

目录 1--nn.Conv2d()参数量的计算 2--nn.Linear()参数量计算 3--显存占用量比较 1--nn.Conv2d()参数量的计算 conv1 nn.Conv2d(in_channels3, out_channels64, kernel_size1) 计算公式&#xff1a; Param in_c * out_c * k * k out_c; in_c 表示输入通道维度&#xff1b…

微信小程序-1:比较两数的大小

程序来源》微信小程序开发教程&#xff08;第二章&#xff09; 主编&#xff1a;黄寿孟、易芳、陶延涛 ISBN&#xff1a; 9787566720788 程序运行结果&#xff1a; <!--index.wxml--> <view class"container"> <text>第一个数字&#xff1a;&…

从认知智能的角度认识ChatGPT的不足

OpenAI的ChatGPT带来了一些令人欣喜的成果&#xff0c;但是从认知智能的角度来看&#xff0c;也有很多不足。今天我就来为大家说一说。首先我会为大家简单介绍人工智能&#xff0c;认知智能&#xff0c;然后再分析ChatGPT的能力和不足&#xff0c;最后分享目前优秀的学术论文以…

网络应用之静态Web服务器

静态Web服务器-返回固定页面数据学习目标能够写出组装固定页面数据的响应报文1. 开发自己的静态Web服务器实现步骤:编写一个TCP服务端程序获取浏览器发送的http请求报文数据读取固定页面数据&#xff0c;把页面数据组装成HTTP响应报文数据发送给浏览器。HTTP响应报文数据发送完…

[11]云计算|简答题|案例分析|云交付|云部署|负载均衡器|时间戳

升级学校云系统我们学校要根据目前学生互联网在线学习、教师教学资源电子化、教学评价过程化精细化的需求&#xff0c;计划升级为云教学系统。请同学们根据学校发展实际考虑云交付模型包含哪些&#xff1f;云部署采用什么模型最合适&#xff1f;请具体说明。9月3日买电脑还是租…

python之并发编程

一、并发编程之多进程 1.multiprocessing模块介绍 python中的多线程无法利用多核优势&#xff0c;如果想要充分地使用多核CPU的资源&#xff08;os.cpu_count()查看&#xff09;&#xff0c;在python中大部分情况需要使用多进程。Python提供了multiprocessing。 multiprocess…

SEO优化:干货技巧分享,包新站1-15天100%收录首页

不管是老域名还是新域名&#xff0c;不管是多久没有收录首页的站&#xff0c;此法周期7-30天&#xff0c;包首页收录&#xff01;本人不喜欢空吹牛逼不实践的理论&#xff0c;公布具体操作&#xff1a;假如你想收录的域名是a.com&#xff0c;那么准备如下材料1.购买5-10个最便宜…

【Tomcat】Tomcat安装及环境配置

文章目录什么是Tomcat为什么我们需要用到Tomcattomcat下载及安装1、进入官网www.apache.org&#xff0c;找到Projects中的project List2、下载之后&#xff0c;解压3、找到tomcat目录下的startup.bat文件&#xff0c;双击之后最后结果出现多少多少秒&#xff0c;表示安装成功4、…

【Python工具篇】Anaconda中安装python2和python3以及在pycharm中使用

背景&#xff1a;已经安装好anaconda、python3、pycharm&#xff0c;因为项目使用的是python2语法&#xff0c;所以需要在anaconda中安装python2&#xff0c;并在pycharm中使用&#xff0c;下面给出步骤。 1. 打开cmd或者是Anaconda Prompt。 下面是anaconda prompt. 2. 查…

hadoop03-MapReduce【尚硅谷】

大数据学习笔记 MapReduce 一、MapReduce概述 MapReduce是一个分布式运算程序的编程框架&#xff0c;是基于Hadoop的数据分析计算的核心框架。 MapReduce处理过程为两个阶段&#xff1a;Map和Reduce。 Map负责把一个任务分解成多个任务&#xff1b;Reduce负责把分解后多任务处…