typora/note/Shell/条件判断语法.md

225 lines
5.5 KiB
Markdown
Raw Permalink Normal View History

2024-12-11 21:48:55 -05:00
### 使用方式分类
- 0 真 1 假
- test 条件表达式
- [ 条件表达式 ],括号两边都有空格
- [[ 条件表达式 ]] 支持正则,括号两边都有空格
### test 条件表达式
```
me@me-EQ59:~/shell_demo$ ls
passwd scripts
me@me-EQ59:~/shell_demo$ test -e passwd
me@me-EQ59:~/shell_demo$ echo $?
0
me@me-EQ59:~/shell_demo$ test -e passwds
me@me-EQ59:~/shell_demo$ echo $?
1
me@me-EQ59:~/shell_demo$
```
### [ 条件表达式 ]
```
me@me-EQ59:~/shell_demo$ ls
passwd scripts
me@me-EQ59:~/shell_demo$ [ -d scripts ]; echo $?
0
me@me-EQ59:~/shell_demo$ [ -d script_dir ]; echo $?
1
me@me-EQ59:~/shell_demo$
me@me-EQ59:~/shell_demo$ ls -l
总计 8
-rwxrwxrwx 1 root root 3311 6月 22 14:30 passwd
drwxrwxr-x 3 me me 4096 6月 22 16:20 scripts
lrwxrwxrwx 1 me me 6 6月 23 14:24 test1 -> passwd
me@me-EQ59:~/shell_demo$ [ -L test1 ]; echo $?
0
me@me-EQ59:~/shell_demo$ mv passwd passwd.bak
me@me-EQ59:~/shell_demo$ ls -l
总计 8
-rwxrwxrwx 1 root root 3311 6月 22 14:30 passwd.bak
drwxrwxr-x 3 me me 4096 6月 22 16:20 scripts
lrwxrwxrwx 1 me me 6 6月 23 14:24 test1 -> passwd
me@me-EQ59:~/shell_demo$ [ -L test1 ]; echo $?
0
me@me-EQ59:~/shell_demo$
```
### [[ 条件表达式 ]]
```
me@me-EQ59:~/shell_demo$ ls
passwd.bak scripts test1
me@me-EQ59:~/shell_demo$ [[ -f passwd.bak ]]; echo $?
0
me@me-EQ59:~/shell_demo$ [[ -f test ]]; echo $?
1
me@me-EQ59:~/shell_demo$
```
### 取反判断
- 表达式前添加 `! `
```
# 判断不存在 test 文件时为真
me@me-EQ59:~/shell_demo$ [[ ! -f test ]]; echo $?
0
me@me-EQ59:~/shell_demo$
```
### 文件判断参数
![fa951c7b58b47ebd846fc00df0ecc70d.png](../../_resources/fa951c7b58b47ebd846fc00df0ecc70d.png)
### 判断文件权限
![e46d8cc96a6a687642c396363f226422.png](../../_resources/e46d8cc96a6a687642c396363f226422.png)
```
me@me-EQ59:~/shell_demo$ test -r /etc/passwd; echo $?
0
me@me-EQ59:~/shell_demo$ test -w /etc/passwd; echo $?
1
me@me-EQ59:~/shell_demo$
```
### 判断文件新旧 && 相等
![105d782b5f5fb4eb79e7fe7583585a65.png](../../_resources/105d782b5f5fb4eb79e7fe7583585a65.png)
- 文件新旧判断
```
me@me-EQ59:~/shell_demo$ ls -l
总计 12
-rwxrwxr-x 1 me me 3311 6月 23 14:39 passwd
-rwxrwxrwx 1 root root 3311 6月 22 14:30 passwd.bak
drwxrwxr-x 3 me me 4096 6月 22 16:20 scripts
lrwxrwxrwx 1 me me 6 6月 23 14:24 test1 -> passwd
me@me-EQ59:~/shell_demo$ test passwd -nt passwd.bak; echo $?
0
me@me-EQ59:~/shell_demo$ test passwd -ot passwd.bak; echo $?
1
me@me-EQ59:~/shell_demo$
```
- 文件是否为同一个文件
```
me@me-EQ59:~/shell_demo$ touch file1
me@me-EQ59:~/shell_demo$ touch file2
me@me-EQ59:~/shell_demo$ test file1 -ef file2; echo $?
1
me@me-EQ59:~/shell_demo$ [ file1 -ef file2 ]; echo $?
1
me@me-EQ59:~/shell_demo$
```
### 判断数字
![c01a0eb6107168c67d46719fee3904d9.png](../../_resources/c01a0eb6107168c67d46719fee3904d9.png)
### 判断字符串
![b0338a4f367376224e8339ba29bec865.png](../../_resources/b0338a4f367376224e8339ba29bec865.png)
### 多重条件判断
- -a 或 && 逻辑与
- -o 或 || 逻辑或
```
me@me-EQ59:~/shell_demo$ [ 1 -eq 1 -a 2 -lt 3 ]; echo $?
0
me@me-EQ59:~/shell_demo$ [ 1 -eq 1 -a 2 -gt 3 ]; echo $?
1
me@me-EQ59:~/shell_demo$ [ 1 -eq 1 ] && [ 2 -lt 3 ]; echo $?
0
me@me-EQ59:~/shell_demo$ [ 1 -eq 1 ] || [ 2 -gt 3 ]; echo $?
0
me@me-EQ59:~/shell_demo$
```
- && 前边为真,后边才会执行
```
me@me-EQ59:~/shell_demo$ id -u
1000
me@me-EQ59:~/shell_demo$ [ $(id -u) -eq 0 ] && echo "admin"
me@me-EQ59:~/shell_demo$ [ ! $(id -u) -eq 0 ] && echo "not admin"
not admin
me@me-EQ59:~/shell_demo$
```
- || 前边为假,后边才会执行
```
me@me-EQ59:~/shell_demo$ id -u
1000
me@me-EQ59:~/shell_demo$ [ $(id -u) -eq 0 ] || echo "not admin"
not admin
me@me-EQ59:~/shell_demo$
```
- 逻辑运算符没有优先级
```
me@me-EQ59:~/shell_demo$ [ $(id -u) -eq 0 ] && echo "admin" || echo "not admin"
not admin
me@me-EQ59:~/shell_demo$
me@me-EQ59:~/shell_demo$ [ 1 -eq 2 ] && ehco AAA || echo BBB && echo CCC
BBB
CCC
me@me-EQ59:~/shell_demo$
```
### 数值比较类 C 风格
- = 表示赋值
- == 表示判断相等
```
me@me-EQ59:~/shell_demo$ ((1==2)); echo $?
1
me@me-EQ59:~/shell_demo$ ((1<=2)); echo $?
0
me@me-EQ59:~/shell_demo$ ((1!=2)); echo $?
0
me@me-EQ59:~/shell_demo$ ((`id -u` == 0)); echo $?
1
me@me-EQ59:~/shell_demo$ ((`id -u`==0)); echo $?
1
me@me-EQ59:~/shell_demo$
```
### 字符串判断
- = 和 == 都表示判断
```
me@me-EQ59:~/shell_demo$ str1="hello world";str2="world"
me@me-EQ59:~/shell_demo$ [ str1 = str2 ];echo $?
1
me@me-EQ59:~/shell_demo$ [ $str1 = $str2 ];echo $?
-bash: [: 参数太多
2
me@me-EQ59:~/shell_demo$ [ "$str1" = "$str2" ];echo $?
1
me@me-EQ59:~/shell_demo$ [ "$str1" == "$str2" ];echo $?
1
me@me-EQ59:~/shell_demo$ [ "$str1" != "$str2" ];echo $?
0
me@me-EQ59:~/shell_demo$
```
### [] 与 [[]]区别
- 判断是空字符串时`[]` 报错 `[[]]`不报错
```
me@me-EQ59:~/shell_demo$ A=
me@me-EQ59:~/shell_demo$ echo $A
me@me-EQ59:~/shell_demo$ [ $A = "hello" ]
-bash: [: =: 需要一元运算符
me@me-EQ59:~/shell_demo$ [[ $A = "hello" ]]
me@me-EQ59:~/shell_demo$ [[ $A = "hello" ]]; echo $?
1
me@me-EQ59:~/shell_demo$
```
- 逻辑运算符 && || 不拆开写的时候需要
```
me@me-EQ59:~/shell_demo$ [ 1 -eq 1 -a 1 -gt 2 ]; echo $?
1
me@me-EQ59:~/shell_demo$ [ 1 -eq 1 && 1 -gt 2 ]; echo $?
-bash: [: 缺少 "]"
2
me@me-EQ59:~/shell_demo$ [[ 1 -eq 1 && 1 -gt 2 ]]; echo $?
1
me@me-EQ59:~/shell_demo$ [[ 1 -lt 1 || 1 -gt 2 ]]; echo $?
1
me@me-EQ59:~/shell_demo$ [ 1 -lt 1 || 1 -gt 2 ]; echo $?
-bash: [: 缺少 "]"
1未找到命令
127
me@me-EQ59:~/shell_demo$
```