- Shell test 命令
- 简介
- 常用参数
- test 的条件关系
- test 的数字对比
- test 的字符串对比
- test 的文件对比
- 实例
- 条件关系实例
- 数字对比实例
- 字符串对比实例
- 文件对比实例
简介
- Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。
- test = [ ] :[ ] 就相当于test命令
- test “$a” = “$b” = [ “$a” = “$b” ]
- 参考文章:https://www.runoob.com/linux/linux-shell-test.html
常用参数
test 的条件关系
| 参数 | 说明 |
|---|---|
| -a | 并且 |
| -o | 或者 |
test 的数字对比
| 参数 | 说明 |
|---|---|
| -eq | 等于 |
| -ne | 不等于 |
| -le | 小于等于 |
| -lt | 小于 |
| -ge | 大于等于 |
| -gt | 大于 |
test 的字符串对比
| 参数 | 说明 |
|---|---|
| = | 等于 |
| != | 不等于 |
| -n 字符串 | 字符串的长度不为零 |
| -z 字符串 | 字符串的长度为零 |
test 的文件对比
| 参数 | 说明 |
|---|---|
| -ef | 文件节点号是否一致(硬链) |
| -nt | 文件1是不是比文件2新 |
| -ot | 文件1是不是比文件2老 |
| -d | 目录 |
| -S | 套结字 |
| -L | 软连接 |
| -e | 存在 |
| -f | 普通文件 |
| -b | 快设备 |
| -c | 字符设备 |
实例
条件关系实例
- 并且关系
[root@ test_test]# a=1 [root@ test_test]# b=2 [root@ test_test]# test "$a" = 1 -a "$b" = 2 && echo "并且关系" 并且关系 [root@ test_test]#
- 或者关系
[root@ test_test]# a=1 [root@ test_test]# b=2 [root@ test_test]# test "$a" = 1 -o "$b" = 0 && echo "或者关系" 或者关系 [root@ test_test]# test "$a" = 0 -o "$b" = 2 && echo "或者关系" 或者关系 [root@ test_test]
数字对比实例
- 等于 与 不等于 关系
[root@ test_test]# a=2 [root@ test_test]# b=2 [root@ test_test]# c=1 [root@ test_test]# [ "$a" -eq "$b" ] && echo "等于关系" 等于关系 [root@ test_test]# [ "$a" -ne "$c" ] && echo "不等于关系" 不等于关系 [root@ test_test]#
- 小于 与 小于等于 关系
[root@ test_test]# a=1 [root@ test_test]# b=1 [root@ test_test]# c=2 [root@ test_test]# [ "$a" -lt "$c" ] && echo "小于关系" 小于关系 [root@ test_test]# [ "$a" -le "$c" ] && echo "小于等于关系" 小于等于关系 [root@ test_test]# [ "$a" -le "$b" ] && echo "小于等于关系" 小于等于关系 [root@ test_test]#
- 大于 与 大于等于 关系
[root@ test_test]# a=2 [root@ test_test]# b=2 [root@ test_test]# c=1 [root@ test_test]# [ "$a" -gt "$c" ] && echo "大于关系" 大于关系 [root@ test_test]# [ "$a" -ge "$c" ] && echo "大于等于关系" 大于等于关系 [root@ test_test]# [ "$a" -ge "$b" ] && echo "大于等于关系" 大于等于关系 [root@ test_test]#
- 代码中的 [ ] 执行基本的算数运算
[root@ test_test]# cat test.sh a=1 b=1 result=$[a+b] # 注意等号两边不能有空格 echo "result 为 $result" [root@ test_test]# sh test.sh result 为 2 [root@ test_test]#
字符串对比实例
- 等于 与 不等于 关系
[root@ test_test]# a=kingdom [root@ test_test]# b=kingdom [root@ test_test]# c=king [root@ test_test]# [ "$a" = "$b" ] && echo "等于关系" 等于关系 [root@ test_test]# [ "$a" != "$c" ] && echo "不等于关系" 不等于关系 [root@ test_test]#
- 为空 与 不为空 关系
[root@ test_test]# a=kingdom [root@ test_test]# b= [root@ test_test]# [ -n "$a" ] && echo "不为空关系" 不为空关系 [root@ test_test]# [ -z "$b" ] && echo "为空关系" 为空关系 [root@ test_test]# [ -z "$a" ] && echo "不为空关系" [root@ test_test]# [ -n "$b" ] && echo "为空关系" [root@ test_test]#
文件对比实例
- 硬链接 与 软链接
- 测试文件
- ln file hard_file : 硬链接
- ln -s file1 soft_file1 :软链接
[root@ test_test]# echo > file [root@ test_test]# ln file hard_file [root@ test_test]# echo > file1 [root@ test_test]# ln -s file1 soft_file1 [root@ test_test]# ll total 16 -rw-r--r-- 2 root root 1 Nov 30 18:11 file -rw-r--r-- 1 root root 1 Nov 30 18:13 file1 -rw-r--r-- 2 root root 1 Nov 30 18:11 hard_file lrwxrwxrwx 1 root root 4 Nov 30 18:13 soft_file1 -> file1 [root@ test_test]#
- 硬链接 与 软链接测试
[root@ test_test]# [ file -ef hard_file ] && echo "硬链接" 硬链接 [root@ test_test]# [ -L soft_file1 ] && echo "软链接" 软链接 [root@ test_test]#
- 新旧文件对比
[root@ test_test]# echo > old [root@ test_test]# echo > new [root@ test_test]# stat -c %Y old 1638267905 [root@ test_test]# stat -c %Y new 1638267931 [root@ test_test]# [ new -nt old ] && echo "new 文件比 old 文件 新" new 文件比 old 文件 新 [root@ test_test]# [ old -ot new ] && echo "old 文件比 new 文件 老" old 文件比 new 文件 老 [root@ test_test]#
- 目录判断
[root@ test_test]# mkdir dir [root@ test_test]# [ -d dir ] && echo "目录判断" 目录判断 [root@ test_test]#
- 套接字判断
- find / -type s 查找套接字文件
[root@ test_test]# find / -type s [root@ test_test]# [ -S /dev/log ] && echo "套接字判断" 套接字判断 [root@ test_test]#
- 文件是否存在判断
[root@ test_test]# echo > file [root@ test_test]# [ -e file ] && echo "文件是否存在判断" 文件是否存在判断 [root@ test_test]#
- 普通文件判断
[root@ test_test]# ll file -rw-r--r-- 2 root root 1 Nov 30 18:11 file [root@ test_test]# [ -f file ] && echo "普通文件判断" 普通文件判断 [root@ test_test]#
- 块设备文件判断
[root@ test_test]# [ -b /dev/vda ] && echo "块设备判断" 块设备判断 [root@ test_test]#
- 字符设备文件判断
[root@ test_test]# [ -c /dev/null ] && echo "字符设备文件判断" 字符设备文件判断 [root@ test_test]#



