Shell test command and []

#############文件判断

test -e /root/file && echo "exist" || echo "not exsit"

test -f /root/file && echo "exist" || echo "not exsit"    #是否存在这个file

test -d /root/file && echo "exist" || echo "not exsit"  #是否存在这个目录

############文件的权限检测

test -r /root/file && echo "read" || echo "not read"

test -w /root/file && echo "write" || echo "not write"

test -x /root/file && echo "exeute" || echo "not exeute"

###########两个文件之间比较

-rw-r--r--  1 root root 1105 Nov 11 01:39 passwd1
-rw-r--r--  1 root root 1120 Nov 11 01:41 passwd2
test passwd1 -nt passwd2 && echo "new" || echo "not new"
not new

test passwd1 -ot passwd2 && echo "old" || echo "not old"

old

test passwd1 ef passwd2 && echo "old" || echo "not old"

passwd1和passwd2是否是用一个文件-》指向同一个Inode

###########两个数字的比较

[root@IPv6 gaole_scripts]# test 1 -eq 2 && echo "1" || echo "2"
2
[root@IPv6 gaole_scripts]# test 1 -ne 2 && echo "1" || echo "2"
1
[root@IPv6 gaole_scripts]# test 1 -ge 2 && echo "1" || echo "2"
2
[root@IPv6 gaole_scripts]# test 1 -le 2 && echo "1" || echo "2"
1
[root@IPv6 gaole_scripts]# test 1 -gt 2 && echo "1" || echo "2"
2
[root@IPv6 gaole_scripts]# test 1 -lt 2 && echo "1" || echo "2"

##############字符串判定

test="123456"

[root@IPv6 gaole_scripts]# test -n $test && echo "empty" || echo "not empty"
empty   #判断字符串为空
[root@IPv6 gaole_scripts]# test -n ${test}="test" && echo "empty" || echo "not empty"
empty #判断字符串为非空

[root@IPv6 gaole_scripts]# test ${test} = "test" && echo "empty" || echo "not empty"
not empty  #判断字符串相等,等号两边要有空格
[root@IPv6 gaole_scripts]# test ${test} != "test" && echo "empty" || echo "not empty"
empty  #判断字符串相等,!等号两边要有空格
[root@IPv6 gaole_scripts]# 

###########多重条件判断


[root@IPv6 gaole_scripts]# test -r error.txt -a -w error.txt && echo "empty" || echo "not empty"
empty   #可读并且可写,-a and
[root@IPv6 gaole_scripts]# test -r error.txt -a -x error.txt && echo "empty" || echo "not empty"
not empty  #可读并且可执行
[root@IPv6 gaole_scripts]# test -r error.txt -o -x error.txt && echo "empty" || echo "not empty"

not empty #可读或者可执行,-o or

[root@IPv6 gaole_scripts]# test ! -r error.txt -o -x error.txt && echo "empty" || echo "not empty"
not empty  #  ! 取反

#############[]中括号里的注意事项

1.【】两边都要有空格,中间的符号和变量直接也要有空格

2.字符串的话,变量和常量最好都加上双引号

3.字符串比较如果有通配符的话,要用两个中括号生效通配符

4.以上的test可以换成【 -z string】类型

比如

[root@IPv6 gaole_scripts]# test -r error.txt -a -w error.txt && echo "empty" || echo "not empty"
empty
[root@IPv6 gaole_scripts]# [ -r error.txt -a -w error.txt ] && echo "empty" || echo "not empty"
empty
[root@IPv6 gaole_scripts]# [ -r error.txt ] && [ -w error.txt ] && echo "empty" || echo "not empty"

[root@IPv6 gaole_scripts]# [ -r error.txt ] || [ -w error.txt ] && echo "empty" || echo "not empty"

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值