Shell学习之条件测试(四)

Shell学习之条件测试

 

目录

逻辑测试

文件测试

数值比较

字符串比较

 

 

逻辑测试

1
2
3
4
5
6
7
8
9
10
11
12
13
格式:
 
  [ 表达式 ]  操作符 [ 表达式2 ] ……
 
  命令1  操作符  命令2 ……
 
常用的操作符 ( 注意:-a和-o放在[]里面用,&&和||放在[]外面用 )
 
    -a  或  &&           逻辑与
 
    -o  或 ||             逻辑或
 
      !               逻辑否

  


文件测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
文件测试
 
格式1:  [  操作符 文件或目录  ] 
 
格式2:test  操作符 文件或目录
 
常用的测试操作符
 
    -d :测试是否为目录( Directory )
 
    -e :测试目录或文件是否存在(Exist)
 
    -f :测试是否为文件(File
 
    -r :测试当前用户是否可读(read)
 
    -w:测试当前用户是否可写(write)
 
    -x :测试当前用户是否可执行(excute)

 

例子:备份Mysql数据库,业务代码没有完善

1
2
3
4
5
6
7
#/bin/bash
back_dir=/var/mysql_back
 
if !test -d $back_dir;then
    mkdir -p $back_dir
fi
echo "开始备份"

  

 


数值比较

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
格式1:[ 整数1 操作符 整数2 ]
 
格式2: test 整数1 操作符 整数2
 
常用的测试操作符
 
    -eq : 等于 (Equal)
 
    -ne : 不等于 (Not Equal)
 
    -gt : 大于(Greater Than)
 
    -lt : 小于 (Lesser Than)
 
    -le : 小于或等于(Lesser or Equal)
 
    -ge : 大于或等于(Greater or Equal)

  

例子

1
2
3
4
5
6
#/bin/bash
if [ $UID -ne 0];then
    echo "没有权限"
    exit
fi
yum -y install httpd

  

 

字符串比较

1
2
3
4
5
6
7
8
9
10
11
12
13
格式1:[ 字符串1 = 字符串2 ]
 
     [ 字符串1 != 字符串2 ]
 
格式2:[ -z 字符串 ]
 
常用的测试操作符
 
  = : 字符串内容相同
 
  != : 字符串内容不同
 
  -z : 字符串内容为空

  

例子

1
2
3
4
5
6
#/bin/bash
if [ $USER = "root"];then
    yum -y install httpd
fi
    echo "没有权限"
    exit

  

 

 

所有表达式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
( EXPRESSION )
              EXPRESSION is true
 
       ! EXPRESSION
              EXPRESSION is false
 
       EXPRESSION1 -a EXPRESSION2
              both EXPRESSION1 and EXPRESSION2 are true
 
       EXPRESSION1 -o EXPRESSION2
              either EXPRESSION1 or EXPRESSION2 is true
 
       -n STRING
              the length of STRING is nonzero
 
       STRING equivalent to -n STRING
 
       -z STRING
              the length of STRING is zero
 
       STRING1 = STRING2
              the strings are equal
 
       STRING1 != STRING2
              the strings are not equal
 
       INTEGER1 -eq INTEGER2
              INTEGER1 is equal to INTEGER2
 
       INTEGER1 -ge INTEGER2
              INTEGER1 is greater than or equal to INTEGER2
 
       INTEGER1 -gt INTEGER2
              INTEGER1 is greater than INTEGER2
 
       INTEGER1 -le INTEGER2
              INTEGER1 is less than or equal to INTEGER2
 
       INTEGER1 -lt INTEGER2
              INTEGER1 is less than INTEGER2
 
       INTEGER1 -ne INTEGER2
              INTEGER1 is not equal to INTEGER2
 
       FILE1 -ef FILE2
              FILE1 and FILE2 have the same device and inode numbers
 
       FILE1 -nt FILE2
              FILE1 is newer (modification date) than FILE2
 
       FILE1 -ot FILE2
              FILE1 is older than FILE2
 
       -b FILE
              FILE exists and is block special
 
       -c FILE
              FILE exists and is character special
 
       -d FILE
              FILE exists and is a directory
 
       -e FILE
              FILE exists
 
       -f FILE
              FILE exists and is a regular file
 
       -g FILE
              FILE exists and is set-group-ID
 
       -G FILE
              FILE exists and is owned by the effective group ID
 
       -h FILE
              FILE exists and is a symbolic link (same as -L)
 
       -k FILE
              FILE exists and has its sticky bit set
 
       -L FILE
              FILE exists and is a symbolic link (same as -h)
 
       -O FILE
              FILE exists and is owned by the effective user ID
 
       -p FILE
              FILE exists and is a named pipe
 
       -r FILE
              FILE exists and read permission is granted
 
       -s FILE
              FILE exists and has a size greater than zero
 
       -S FILE
              FILE exists and is a socket
 
       -t FD  file descriptor FD is opened on a terminal
 
       -u FILE
              FILE exists and its set-user-ID bit is set
 
       -w FILE
              FILE exists and write permission is granted
 
       -x FILE
              FILE exists and execute (or search) permission is granted

  

 

posted @   -零  阅读(283)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示