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)

三、组合判断

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

相关推荐
Wnq100724 小时前
智能巡检机器人在化工企业的应用研究
运维·计算机视觉·机器人·智能硬件·deepseek
tf的测试笔记7 小时前
测试团队UI自动化实施方案
运维·自动化
TDD_06287 小时前
【运维】Centos硬盘满导致开机时处于加载状态无法开机解决办法
linux·运维·经验分享·centos
x66ccff7 小时前
vLLM 启动 GGUF 模型踩坑记:从报错到 100% GPU 占用的原因解析
linux
头孢头孢7 小时前
k8s常用总结
运维·后端·k8s
遇码7 小时前
单机快速部署开源、免费的分布式任务调度系统——DolphinScheduler
大数据·运维·分布式·开源·定时任务·dolphin·scheduler
William.csj7 小时前
Linux——开发板显示器显示不出来,vscode远程登录不进去,内存满了的解决办法
linux·vscode
爱编程的王小美8 小时前
Docker基础详解
运维·docker·容器
KeithTsui8 小时前
GCC RISCV 后端 -- 控制流(Control Flow)的一些理解
linux·c语言·开发语言·c++·算法
森叶8 小时前
linux如何与windows进行共享文件夹开发,不用来回用git进行拉来拉去,这个对于swoole开发者来说特别重要
linux·git·swoole