首页 > 系统相关 >Shell脚本---条件判断

Shell脚本---条件判断

时间:2024-05-30 21:04:59浏览次数:19  
标签:脚本 tmp file1 Shell shello1 echo --- root localhost

1. 条件判断语法结构

思考:何为真(True)?何为(False)?

  • 格式1:test 条件表达式
  • 格式2:[ 条件表达式 ]
  • 格式3:[[ 条件表达式 ]] 支持正则 =~

特别说明:
1)[ 中括号两边都有空格 ]
2)[[ 中括号两边都有空格 ]]
3) 更多判断, man test 去查看,很多的参数都用来进行条件判断

2. 条件判断相关参数

问:你要判断什么?
答:我要判断文件类型,判断文件新旧,判断字符串是否相等,判断权限等…

2.1 判断文件类型

判断参数含义
-e判断文件是否存在(任何类型文件)
-f判断文件是否存在并且是一个普通文件
-d判断文件是否存在并且是一个目录
-L判断文件是否存在并且是一个软连接文件
-b判断文件是否存在并且是一个块设备文件
-S判断文件是否存在并且是一个套接字文件
-c判断文件是否存在并且是一个字符设备文件
-p判断文件是否存在并且是一个命令管道文件
-s判断文件是否存在并且是一个非空文件(有内容)

示例:

[root@localhost shello1]# ll
总用量 4
-rwxr-xr-x. 1 root root 70 4月  17 14:41 first_shell.sh
drwxr-xr-x. 2 root root  6 4月  18 13:34 test1
[root@localhost shello1]# touch file1
[root@localhost shello1]# echo hello > file2
[root@localhost shello1]# ll
总用量 8
-rw-r--r--. 1 root root  0 4月  18 13:39 file1
-rw-r--r--. 1 root root  6 4月  18 13:40 file2
-rwxr-xr-x. 1 root root 70 4月  17 14:41 first_shell.sh
drwxr-xr-x. 2 root root  6 4月  18 13:34 test1
[root@localhost shello1]# mkdir dir1
[root@localhost shello1]# test -e ./file1
[root@localhost shello1]# echo $?
0
[root@localhost shello1]# test -e ./test1/
[root@localhost shello1]# echo $?
0
[root@localhost shello1]# test -e ./test1/
[root@localhost shello1]# echo $?
0
[root@localhost shello1]# test -e ./test2
[root@localhost shello1]# echo $?
1
[root@localhost shello1]# [ -d ./dir1/ ];echo $?
0
[root@localhost shello1]# [ -d ./file1/ ];echo $?
1
[root@localhost shello1]# ln -s file1 test2
[root@localhost shello1]# ll
总用量 8
drwxr-xr-x. 2 root root  6 4月  18 13:40 dir1
-rw-r--r--. 1 root root  0 4月  18 13:39 file1
-rw-r--r--. 1 root root  6 4月  18 13:40 file2
-rwxr-xr-x. 1 root root 70 4月  17 14:41 first_shell.sh
drwxr-xr-x. 2 root root 19 4月  18 13:43 test1
lrwxrwxrwx. 1 root root  5 4月  18 13:44 test2 -> file1
[root@localhost shello1]# [ -L ./test2 ];echo $?
0
[root@localhost shello1]# rm -rf file1
[root@localhost shello1]#
[root@localhost shello1]# ll
总用量 8
drwxr-xr-x. 2 root root  6 4月  18 13:40 dir1
-rw-r--r--. 1 root root  6 4月  18 13:40 file2
-rwxr-xr-x. 1 root root 70 4月  17 14:41 first_shell.sh
drwxr-xr-x. 2 root root 19 4月  18 13:43 test1
lrwxrwxrwx. 1 root root  5 4月  18 13:44 test2 -> file1
[root@localhost shello1]# [ -L ./test2 ];echo $?
0
[root@localhost shello1]# [[ -f ./file2 ]];echo $?
0
[root@localhost shello1]# ll
总用量 8
drwxr-xr-x. 2 root root  6 4月  18 13:40 dir1
-rw-r--r--. 1 root root  6 4月  18 13:40 file2
-rwxr-xr-x. 1 root root 70 4月  17 14:41 first_shell.sh
drwxr-xr-x. 2 root root 19 4月  18 13:43 test1
lrwxrwxrwx. 1 root root  5 4月  18 13:44 test2 -> file1
[root@localhost shello1]# [[ -f ./test2 ]];echo $?
1

2.2 判断文件权限

判断参数含义
-r当前用户对其是否可读
-W当前用户对其是否可写
-x当前用户对其是否可执行
-u是否有suid,高级权限冒险位
-g是否有sgid,高级权限强制位
-k是否t位,高级权限粘滞位

示例:

[root@localhost shell01]# ll
总用量 8
-rwxr-xr-x. 1 root root 114 4月  17 15:09 1.sh
-rw-r--r--. 1 root root   9 4月  17 16:24 ip.txt
[root@localhost shell01]# test -r 1.sh;echo $?
0

2.3 判断文件新旧

说明:这里的新旧指的是文件的修改时间

判断参数含义
file1 -nt file2比较file1是否比file2新
file1 -ot file2比较file1是否比file2旧
file1 -et file2比较是否为同一个文件,或者用于判断硬连接,是否指向同一个inode

示例:

[root@localhost tmp]# ll
总用量 16
-rw-r--r--. 1 root root   12 4月  15 17:43 11.txt
-rw-r--r--. 1 root root  385 4月  15 17:34 2.txt
-rw-r--r--. 1 root root    0 4月  20 16:09 file1
-rw-r--r--. 1 root root   22 4月  15 20:05 file2
-rw-r--r--. 1 root root 2198 4月  15 14:13 passwd
drwxr-xr-x. 4 root root   79 4月  18 13:45 shello1
[root@localhost tmp]# test file1 -nt file2;echo $?
0
[root@localhost tmp]# test file1 -ot file2;echo $?
1
[root@localhost tmp]# test file2 -ot file1;echo $?
0
[root@localhost tmp]# [ file1 -ef file2 ];echo $?
1
[root@localhost tmp]# cat file2
heima itcast
999
ooo
[root@localhost tmp]# cat file1
hello

2.4 判断整数

判断参数含义
-eq相等
-ne不相等
-gt大于
-lt小于
-ge大于等于
-le小于等于

示例:

[root@localhost tmp]# [ 1 -eq 2 ];echo $?
1
[root@localhost tmp]# [ 1 -ne 2 ];echo $?
0
[root@localhost tmp]# [ 1 -le 2 ];echo $?
0

2.5 判断字符串

判断参数含义
-z判断是否为空字符串,字符串长度为0则成立
-n判断是否为非空字符串,字符串长度不为0则成立
string1=string2判断字符串是否相等
string1=!string2判断字符串是否不相等

示例:

[root@localhost tmp]# test -z "hello";echo $?
1
[root@localhost tmp]# test -n "hello";echo $?
0
[root@localhost tmp]# test -n " ";echo $?
0
[root@localhost tmp]# test -n "";echo $?
1
[root@localhost tmp]# test -z "";echo $?
0
[root@localhost tmp]# test "hello" = "wold";echo $?
1
[root@localhost tmp]# test "hello" != "wold";echo $?
0

2.6 多重条件判断

判断符号含义举例
-a 和 &&逻辑与[ 1 -eq 1 -a 1 -ne 0 ] [ 1 -eq 1 ] && [ 1 -ne 0 ]
-o 和 ||逻辑或[ 1 -eq 1 -o 1 -ne 1 ] [ 1 -eq 1 ]

特别说明:
&& 前面的表达式为真,才会执行后面的代码
|| 前面的表达式为假,才会执行后面的代码
; 只用于分割命令和表达式

数值比较示例:

[root@localhost tmp]# id -u
0
[root@localhost tmp]# [ $(id -u) -eq 0 ] && echo "admin"
admin
[root@localhost tmp]# su - aileen
上一次登录:一 4月 15 22:06:54 CST 2024:0 上
[aileen@localhost ~]$ [ $(id -u) -eq 0 ] && echo "admin"
[aileen@localhost ~]$ [ $(id -u) -eq 0 ] || echo "is not admin"
is not admin
[aileen@localhost ~]$ id -u
1000
[aileen@localhost ~]$ [ $(id -u) -eq 0 ] && echo "admin" || echo "is not admin"
is not admin
[aileen@localhost ~]$ exit
登出
[root@localhost tmp]#  [ $(id -u) -eq 0 ] && echo "admin" || echo "is not admin"
admin
[root@localhost tmp]# [ 1 -eq 2 ] && echo AAA || echo BBB && echo CCC
BBB
CCC
[root@localhost tmp]# [ 1 -eq 2 ] || echo AAA && echo BBB && echo CCC
AAA
BBB
CCC

类C风格的数值比较示例:

注意:在 (( )) 中,= 表示赋值;== 表示判断

[root@localhost tmp]# (( 1==1 ));echo $?
0
[root@localhost tmp]# (( 1>=1 ));echo $?
0
[root@localhost tmp]# (( 1>=2 ));echo $?
1
[root@localhost tmp]# (( 1=2 ));echo $?
-bash: ((: 1=2 : 尝试给非变量赋值 (错误符号是 "=2 ")
1
[root@localhost tmp]# (( A1=2 ));echo $?
0
[root@localhost tmp]# echo $A1
2
[root@localhost tmp]# ((1!=2 ));echo $?
0
[root@localhost tmp]# ((1!==2 ));echo $?
-bash: ((: 1!==2 : 语法错误: 期待操作数 (错误符号是 "=2 ")
1
[root@localhost tmp]# ((1>2 ));echo $?
1

字符串比较示例:

注意:双引号引起来,看作一个整体; = 和 == 在 [ 字符串 ] 比较中都表示判断

[root@localhost tmp]# A=hello;B=world;[ "$A" = "$B" ];echo $?
1
[root@localhost tmp]# A=hello;B=world;[ "$A" != "$B" ];echo $?
0
[root@localhost tmp]# A=hello;B=world;[ "$A" == "$B" ];echo $?
1

3. 判断符号 [ ] 和 [[ ]] 的区别

[root@localhost tmp]# A=
[root@localhost tmp]# echo $A

[root@localhost tmp]# test "$A" = "hello"; echo $?
1
[root@localhost tmp]# [ $A = hello ];echo $?
-bash: [: =: 期待一元表达式
2
[root@localhost tmp]# [ "$A" = "hello" ];echo $?
1
[root@localhost tmp]# [[ $A = hello ]];echo $?
1

4. 逻辑运算符总结

  • 符号 ; 和 && 和 || 都可以用来分割命令或者表达式
  • 分号( ; )完全不考虑前面的语句是否正确执行,都会执行分号后面的内容
  • &&符号,需要考虑&&前面的语句的正确性,前面语句正确执行才会执行&&后的内容;反之亦然
  • || 符号,需要考虑 || 前面的语句的非正确性,前面语句执行错误才会执行 || 后的内容;反之亦然
  • 如何 && 和 || 一起出现,从左往右一次看,按照以上原则

标签:脚本,tmp,file1,Shell,shello1,echo,---,root,localhost
From: https://blog.csdn.net/qq_54591401/article/details/139333072

相关文章

  • 7-1 sdut-C语言实验-母牛的故事
    代码长度限制16KB时间限制400ms内存限制64MB栈限制8192KB题目:有一对夫妇买了一头母牛,它从第2年起每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?输入格式:输入为一个整数n(0<n<55)输出格......
  • Debug-013-el-loading中显示倒计时时间
    前言:            今天实现一个小小的优化,业务上是后端需要从设备上拿数据,所以前端需要不断调用一个查询接口,直到后端数据获取完毕,前后端根据一个ending字段为true判断停止调用查询接口。由于这个查询时间比较久,所以需要一个laoding效果。优化:前端除了根据后......
  • JVM学习-字节码指令集(二)
    对象的创建与访问指令创建指令虽然类实例和数组都是对象,但Java虚拟机对类实例和数组的创建和操作使用了不同的字节码指令创建类实例指令:new它接收一个操作数,指向常量池的索引,表示要创建的类型,执行完成后,将对象的引用压入操作数栈创建数组的指令创建数组的指令:newarray,......
  • 程序分享--常见算法/编程面试题:不使用额外数组空间,原地移除数组中给定元素
    关注我,持续分享逻辑思维&管理思维&面试题;可提供大厂面试辅导、及定制化求职/在职/管理/架构辅导;有意找工作的同学,请参考博主的原创:《面试官心得--面试前应该如何准备》,《面试官心得--面试时如何进行自我介绍》, 《做好面试准备,迎接2024金三银四》。或关注博主免费专栏【程序......
  • MPI(二)- 进程调度,绑定
    单节点情况下不显式绑定CPU核心MPI运行时环境会依赖操作系统来管理MPI进程与CPU核心的映射和调度。操作系统会尝试均匀分配负载,但可能会出现缓存污染、上下文切换开销增加以及NUMA访问延迟等问题。默认调度操作系统的默认调度器会将进程分配到可用的CPU核心上,尝试均匀分......
  • 模型节点操作学习笔记(Appendix)实验1 -- Tflite int8 删除最后的Round节点 (持续更新)
    背景如下:我要删除Round节点,同时看了一下,Dequantize和Quantize也是没有必要的。所以最好一起删除。原始项目地址:PINTO0309/hand-gesture-recognition-using-onnx:ThisisahandgesturerecognitionprogramthatreplacestheentireMediaPipeprocesswithONNX.Simultane......
  • python-argparse用法简介
    1.argparse介绍argparse是Python标准库中用于解析命令行参数的模块。它提供了一种简洁而灵活的方式来处理命令行参数,包括选项(可选参数)和位置参数(必需参数)2.argparse基本使用点击查看代码importargparse#1.创建ArgumentParser对象,在创建ArgumentParser对象时,可以传入......
  • 鸿蒙HarmonyOS实战-Web组件(页面跳转和浏览记录)
    ......
  • Large Language Models as Data Augmenters for Cold-Start Item Recommendation论文
    LargeLanguageModelsasDataAugmentersforCold-StartItemRecommendation论文阅读笔记Abstract​ LLM的推理和泛化能力可以帮助我们更好地理解用户的偏好和项目特征。我们建议利用LLM作为数据增强器,来弥补在训练过程中对冷启动项目的知识差距。我们使用LLM根据用户历史行......
  • 在GitHub上学黑客 --- 黑客成长技术清单
    在GitHub上学黑客—黑客成长技术清单1黑客的工具包这个开源项目是黑客的多合一工具包,包含了各种黑客工具,比如逆向工程、老鼠工具、SQL注入工具等等。声明:该开源项目仅限学习使用,勿用与从事违法活动。【黑客进阶资源资料包】开源地址:https://github.com/Z4nzu/hacking......