[ condition ] (注意 condition 前后要有空格)
注意:条件非空即为true,[ atguigu ]返回true,[] 返回false
两个整数之间的比较
| 运算符 | 说明 | 
|---|---|
| == | 相等,用于比较两个数,相同则返回true | 
| != | 不相等,用于比较两个数,不相同则返回true | 
| = | 字符串比较 | 
| -lt | 小于(less than) | 
| -le | 小于等于(less equal) | 
| -eq | 等于(equal) | 
| -gt | 大于(greater than) | 
| -ge | 大于等于(greater equal) | 
| -ne | 
按照文件权限进行判断
| 说明 | |
|---|---|
| -r | 有读的权限(read) | 
| -w | 有写的权限(write) | 
| -x | 有执行的权限(execute) | 
按照文件类型进行判断
| 说明 | |
|---|---|
| -f | 文件存在并且是一个常规文件(file) | 
| -e | 文件存在(existence) | 
| -d | 文件存在并是一个目录 | 
[root@slave2 testshell]# [ 22 -le 23 ] [root@slave2 testshell]# echo $? 0 [root@slave2 testshell]# [ 22 -ge 23 ] [root@slave2 testshell]# echo $? 1 [root@slave2 testshell]# ll total 16 -rw-r--r--. 1 root root 85 Jan 18 08:18 addwords.sh -rwxrwxrwx. 1 root root 318 Jan 18 21:24 ArithmeticOperation.sh -rw-r--r--. 1 root root 11 Jan 18 08:18 banzhang.txt -rwxrwxrwx. 1 root root 54 Jan 18 10:05 parameter.sh [root@slave2 testshell]# [ -w addwords.sh ] [root@slave2 testshell]# echo $? 0 [root@slave2 testshell]# [ -r addwords.sh ] [root@slave2 testshell]# echo $? 0 [root@slave2 testshell]# [ -e addwords.sh ] [root@slave2 testshell]# echo $? 0 [root@slave2 testshell]# [ -f addwords.sh ] [root@slave2 testshell]# echo $? 0 [root@slave2 testshell]# [ -d addwords.sh ] [root@slave2 testshell]# echo $? 1 [root@slave2 testshell]# [ -x addwords.sh ] [root@slave2 testshell]# echo $? 1
&&:表示前一条命令执行成功时,才执行后一条命令
||:表示上一条命令执行失败后,才执行下一条命令
[root@slave2 testshell]# [ -x addwords.sh ] && echo ok || echo ‘not ok‘ not ok [root@slave2 testshell]# [ -r addwords.sh ] && echo ok || echo ‘not ok‘ ok
原文:https://www.cnblogs.com/zxbdboke/p/10416072.html