Shell——条件判断

目录

一、格式

二、常用条件判断

三、组合判断


一、格式

test+表达式或者使用中括号[](中括号前后一定要有空格,Linux中没有空格一般都会看作一个整体变量

复制代码
[root@hadoop-master sh_test]# a=hi
[root@hadoop-master sh_test]# test $a = hi
[root@hadoop-master sh_test]# echo $?
0

[root@hadoop-master sh_test]# test $a = hello
[root@hadoop-master sh_test]# echo $?
1

# 注意等号需要空格

[root@hadoop-master sh_test]# [ $a = hello ]
[root@hadoop-master sh_test]# echo $?
1

二、常用条件判断

(一)两整数

  1. -eq(equal):等于

  2. -lt(less than):小于

  3. -gt(greater than):大于

  4. -ne(not equal):不等于

  5. -le(less equal):小于等于

  6. -ge(greater equal):大于等于

  7. 若是字符串,使用"="或者"!="判断是否相等

    [root@hadoop-master sh_test]# [ 2 < 8 ]
    -bash: 8: 没有那个文件或目录

    [root@hadoop-master sh_test]# [ 2 -ge 8 ]
    [root@hadoop-master sh_test]# echo $?
    1

(二)文件权限

  1. -r:读(read)
  2. -w:写(write)
  3. -x:执行(execute)
javascript 复制代码
[root@hadoop-master sh_test]# ll
总用量 8
-rwxr-xr-x. 1 root root  35 10月 21 15:13 add.sh
-rwxr-xr-x. 1 root root 112 10月 21 11:26 hello.sh
-rw-r--r--. 1 root root   0 10月 21 16:02 test.sh

[root@hadoop-master sh_test]# [ -x add.sh ]
[root@hadoop-master sh_test]# echo $?
0

[root@hadoop-master sh_test]# [ -x test.sh ]
[root@hadoop-master sh_test]# echo $?
1

(三)文件类型

  1. -e:文件存在(existence)
  2. -f:文件存在并且是一个常规文件(file)
  3. -d:文件存在并且是一个目录(directory)

三、组合判断

使用&&与||将多个条件判断相连起来,其中&&的优先级更高一些

相关推荐
laocui16 分钟前
Ubuntu22.04开机运行程序
linux·运维·服务器
遇见火星19 分钟前
Linux 防火墙 firewalld 实战配置教程!
linux·防火墙·firewalld
卡戎-caryon22 分钟前
【Linux网络与网络编程】12.NAT技术&&内网穿透&&代理服务
linux·运维·服务器·网络·网络协议·https
我的老子姓彭24 分钟前
LWIP的Socket接口
运维·服务器·网络
朱包林25 分钟前
day10-别名-MD5,aide-堡垒机
linux·运维·安全·ubuntu·centos·云计算
好吃的肘子34 分钟前
Zookeeper 入门(二)
linux·分布式·zookeeper
Javis2111 小时前
【Linux高级全栈开发】2.1.3 http服务器的实现
linux·服务器·http
GoWjw1 小时前
Linux虚拟文件系统(1)
运维·服务器·数据库
SunTecTec2 小时前
Linux grep -r 查找依赖包是否存在依赖类 Class
linux·运维·服务器
Johny_Zhao2 小时前
AI+自动化测试系统方案:网络设备与网络应用智能测试
linux·网络·人工智能·python·网络安全·docker·ai·信息安全·云计算·ansible·shell·cisco·huawei·系统运维·itsm·华三·deepseek