版权声明:silly8543 https://blog.csdn.net/cen50958/article/details/90054990
1.语法
- test <测试表达式>
- [ <测试表达式> ]
- [[ <测试表达式> ]]
说明:
格式1和格式2等价
格式3为扩展的test命令
[[ ]]可以使用通配符进行模式匹配,&&、||、>、<等操作符可以应用于[[]]中,但不能应用于[]中
2. 常用测试操作符
-
查看系统测试操作符
man test
-
文件测试操作符
作符 说明 -f 文件(file) 文件存在且为普通文件则真 -d 文件(directory) 文件存在且为目录文件则真 -s 文件(size) 文件存在且不为空(文件大小非0)则真 -e 文件(exist) 文件存在则真(与f区别,不判别是否为普通文件) -r 文件(read) 文件存在且可读则真 -w 文件(write) 文件存在且可写则真 -x 文件(execute) 文件存在且可执行则真 -L 文件(link) 文件存在且为连接文件则真 file1 -nt file2(newer than) 文件file1比file2新则真 file1 -ot f2(older than) 文件file1比file2旧则真 -
字符串测试操作符
作符 说明 -z “字符串” 长度为0则真 -n “字符串” 长度不为0则真 “字符串1” = “字符串2” 字符串1等于字符串2则真,可用== “字符串1” != “字符串2” 字符串1不等于字符串2则真 -
整数比较操作符
[]中使用 (())和[[]]使用 说明 -eq == 相等 -ne != 不相等 -gt > 大于 -ge >= 大于等于 -lt < 小于 -le <= 小于等于 -
逻辑操作符
[]中使用 (())和[[]]使用 说明 -a && and -o || or ! ! 非