栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Shell test 命令

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Shell test 命令

Shell test 命令

文章目录
  • 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]# 



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/618666.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号