shell条件判断之test命令实例详解

本文详细介绍了Bash shell中的test命令及其简化形式[ ]的使用方法。通过具体示例展示了如何利用test命令进行数值比较、字符串比较、文件属性判断等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基本格式:
test expression

expression为test命令构造的表达式。
这里expression是test命令可以理解的任何有效表达式,该简化格式将是读者可能会踫见的最常用格式
返回值:
test命令或者返回0(真) 或者返回1(假).

test可理解的表达式类型分为四类:

  •     表达式判断
  •     字符串比较
  •     数字比较
  •     文件比较

1)判断表达式

if test  (表达式为真)
if test !表达式为假
test 表达式1 –a 表达式 2                两个表达式都为真
test 表达式1 –o 表达式2                 两个表达式有一个为真

 

2)判断字符串

test –n 字符串                          字符串的长度非零
test –z 字符串                           字符串的长度为零
test 字符串1=字符串 2          字符串相等
test 字符串1 !=字符串2         字符串不等

 

3)判断整数

test 整数1 –eq 整数2                       整数相等
test 整数 1 –ge 整数2                      整数1大于等于整数2
test 整数1 –gt 整数 2                       整数1大于整数2
test 整数1 –le 整数 2                       整数1小于等于整数2
test 整数1 –lt 整数 2                         整数1小于整数2
test 整数1 –ne 整数 2                      整数1不等于整数2

 
4)判断文件

test  File1 –ef  File2                            两个文件具有同样的设备号和i结点号
test  File1 –nt  File2                            文件1比文件2 新
test  File1 –ot  File2                            文件1比文件2 旧
test –b File            文件存在并且是块设备文件
test –c File            文件存在并且是字符设备文件
test –d File            文件存在并且是目录
test –e File            文件存在
test –f File            文件存在并且是正规文件
test –g File            文件存在并且是设置了组ID
test –G File            文件存在并且属于有效组ID
test –h File            文件存在并且是一个符号链接(同-L)
test –k File             文件存在并且设置了sticky位
test –b File            文件存在并且是块设备文件
test –L File            文件存在并且是一个符号链接(同-h)
test –o File            文件存在并且属于有效用户ID
test –p File            文件存在并且是一个命名管道
test –r File            文件存在并且可读
test –s File            文件存在并且是一个套接字
test –t FD                文件描述符是在一个终端打开的
test –u File            文件存在并且设置了它的set-user-id位
test –w File            文件存在并且可写
test –x File            文件存在并且可执行

 

test xxx 可以简写成 [  xxx  ] 的形式。

注意:在使用"["简写test时,左中括号后面的空格和右括号前面的空格是必需的,如果没有空格,Shell不可能辨别表达式何时开始何时结束。

也就是说

    test option file

可以全部改写成:

    [ option file ]

例如:

 test –w File
 
改写成    

[ –w File ]    

【示例】

当我要检测系统上面某些档案或者是相关的属性时,利用 test 这个指令来工作, 真是好用得不得了。OK!现在我们就利用 test 来帮我们写几个简单的例子。test的命令通常都是与条件判断if,while等配合使用的,下的例子涉及到if语句的使用,将在后面进行详细的介绍,这里关注test的用法就好。
例1.比较两个数大小
复制代码
#!/bin/bash

# Program:

#       This program will show the use of test command

# History:

# 2015/1/5 Alex First release

echo "Using $0 $1 $2 to compare the parameter $1 and $2 "

#if test $1 -eq $2

if [ $1 -eq $2 ] #equal to if test $1 -eq $2

then echo "NO.1 = NO.2"

elif test $1 -gt $2

then echo "NO.1 > NO.2"

else echo "NO.1 < NO.2"

fi

exit 0

测试:
deyuy/bin/my_shell >> test.sh 2 2

Using test.sh 2 2 to compare the parameter 2 and 2

NO.1 = NO.2

deyuy/bin/my_shell >> test.sh 10 100

Using test.sh 10 100 to compare the parameter 10 and 100

NO.1 < NO.2

注意:左中括号后面的空格和右括号前面的空格
复制代码
 
例2.查找/root/目录下是否存在该文件
复制代码
#!/bin/bash

# Program:

#       This program will show the use of test command

# History:

# 2015/1/5 Alex First release

echo "Using $0 $1 to check a file's existence"

#if test -e $1

if [ -e $1 ] # equal to if test -e $1

then echo "the file is exist!"

else echo "the file is not exist!"

fi

exit 0

测试:
deyuy/bin/my_shell >> ls

app1      fun1.sh   hello.sh  test.sh   test2.sh  var.sh

deyuy/bin/my_shell >> chmod u+x test2.sh

deyuy/bin/my_shell >> test2.sh test.sh

Using test2.sh test.sh to check a file's existence

the file is exist!

deyuy/bin/my_shell >> test2.sh hhhhhhh.sh

Using test2.sh hhhhhhh.sh to check a file's existence

the file is not exist!

注意:左中括号后面的空格和右括号前面的空格
复制代码
很有趣的例子吧!您可以自行再以其它的案例来撰写一下可用的功能!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值