[Linux] Shell 学习(三) test命令的使用

概述

test命令用于检查文件类型和比较值,并且根据表达式的值来确定状态并退出。所以如果只执行test命令,是不会有任何输出的,可以使用echo $?来查看上次执的状态码(0表示成功,其他值则表示不成功)。用在Shell Script中可以作为判读语句和if..then一起使用,如果在终端输出打印,则经常和管道命令&&||一起使用,如:

$ test 1 -ge 2 && echo "yes" || echo "no"
no

判断表达式

shell中提供了逻辑运算符-a, -o, !,表示与,或,非:

test Expression     # 表达式是否为true
test ! Expresssion  # 表达式是否为false
test Expression1 -a Expression2  # 表达式1和2都是否为true
test Expression1 -o Expression2  #是否表达式1或2为true

如:

# 如果1>0 且 2>3,输出true,否则false
$ test 1 -gt 0 -a 2 -gt 3 &&echo "true" || echo "false"
false
# 如果1>0 或 2>3,输出true,否则false
$ test 1 -gt 0 -o 2 -gt 3 &&echo "true" || echo "false"
true
# 如果1>1或2>3,输出true,否则false
$ test 1 -gt 1 -o 2 -gt 3 &&echo "true" || echo "false"
false

判断字符串

test命令也可以判断字符串长度是否为0,还可以比较两个字符串:

test -n String    # 字符串长度不为0则为true
test String       #等效-n参数
test -z String    # 字符串长度为0,则为true
test String1 = String2   #字符串1和2相等,则为true注意空格
test String != String2    # 字符串1和字符串2不等,则为true

如:

$ test "" && echo "true" || echo "false"
false
$ test "12123" && echo "true" || echo "false"
true
$ test -n "" && echo "true" || echo "false"
false
$ test -z "" && echo "true" || echo "false"
true
$ test "123" = "132" && echo "true" || echo "false"
false
$ test "123" = "123" && echo "true" || echo "false"
true
$ test "123" != "132" && echo "true" || echo "false"
true
$ test "123" != "123" && echo "true" || echo "false"
false

判断数字

test命令可用来比较两个数的大小:

test Integer1 -eq Integer2    #数字1等于数字2,则为true
test Integer1 -ge Integer2    #数字1大于等于数字2,则为true
test Integer1 -gt Integer2    #数字1大于数字2,则为true
test Integer1 -le Integer2    #数字1小于等于数字2,则为true
test Integer1 -lt Integer2    #数字1小于数字2,则为true
test Integer1 -ne Integer2    #数字1不等于数字2,则为true

如:

$ test 1 -le 0 && echo "true" || echo "false"
false
$ test 1 -ge 0  && echo "true" || echo "false"
true
$ test 1 -gt 0  && echo "true" || echo "false"
true
$ test 1 -lt 0  && echo "true" || echo "false"
false
$ test 1 -ne 0  && echo "true" || echo "false"
true

判断文件

test -b FILE    #文件是否存在并且是块设备

test -c FILE    #文件是否存在并且是字符文件

test -d FILE    #文件是否存在并且是目录(常用)

test -e FILE    #文件是否存在(常用)

test -f FILE    #文件是否存在并且是普通文件(常用)

test -h FILE    #文件是否存在并且是连接文件

test -L FILE    #等效于-h

test -p FILE    #文件是否存在且为一个管道文件(FIFO)

test -r FILE    #文件是否存在并且具有可读权限

test -s FILE    #文件是否存在并且文件大小不为0

test -S FILE    #文件是否存在并且是一个Socket

test -t FD    #文件描述符FD是否在终端打开

test -w FILE    #文件是否存在并且具有可写权限

test -x FILE    #文件是否存在并且具有可执行权限

如:

# 当前目录下存在一个android文件夹
$ ls -l
total 4
drwxrwxr-x 2 jiayongqiang jiayongqiang 4096 626 16:50 android
$ test -e android && echo "true" || echo "false"
true
$ test -b android && echo "true" || echo "false"
false
$ test -c android && echo "true" || echo "false"
false
$ test -d android && echo "true" || echo "false"
true
$ test -e android && echo "true" || echo "false"
true
$ test -f android && echo "true" || echo "false"
false
$ test -h android && echo "true" || echo "false"
false
$ test -r android && echo "true" || echo "false"
true
$ test -s android && echo "true" || echo "false"
true
$ test -S android && echo "true" || echo "false"
false
$ test -w android && echo "true" || echo "false"
true
$ test -x android && echo "true" || echo "false"
true

拓展:判断符号[]的使用

在shell script中,除了经常使用test命令来判断表达式外,还可以使用判断符号[],不过需要注意一下几点:

  • 1.判断符号中的每个组件必须使用空格来分隔;
  • 2.判断符号中的变量使用双引号扩起来;
  • 3.判读符号中的常量使用双引号或者单引号扩起来。
    如:
$ name=shell
$ [ "$name" == "shell" ] && echo "true" || echo false
true

判断符号中也支持逻辑运算符-a,-o,!,以及test命令中的一些判断参数,如:

$ [ "$name" == "shells" -a 1 -gt 0 ] && echo true || echo false 
false
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值