Shell test 命令

Shell 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]# 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值