test 命令和可以代替它的方括号

普通的test可以用[]来代替,

compound test可以用[[]]代替。

如果是compound test就可以理解为metacharecter matching的支持,此时如果要精确的比较,就要加上双引号。

 

很奇怪的是,在fedora12上,

$name = zhangqing
$[ $name = [zZ]hangqing ]
$echo $?                                0
$[ $name=[zZ]hangqing ]
$echo $?                                1

实验是证明一定要用空格隔开

 

经过确认:

Table 14.3. The test Command Operators

Test Operator

Tests True If

String Test

[ string1 = string2 ]

[ string1==string2 ]


String1 is equal to String2 (space surrounding = required).

(Can be used instead of the single = sign on bash versions 2.x.)

[ string1 != string2 ]

String1 is not equal to String2 (space surrounding != required).

[ string ]

String is not null.

[ –z string ]

Length of string is zero.

[ –n string ]

Length of string is nonzero.

[ –l string ]

Length of string (number of characters).

 

EXAMPLE

test –n $word      or     [ –n $word ]
test tom = sue      or     [ tom = sue ]

Logical Test

[ string1 –a string1 ]

Both string1 and string2 are true.

[ string1 –o string2 ]

Either string1 or string2 is true.

[ ! string1 ]

Not a string1 match.

Logical Test (Compound Test)[a]

[[ pattern1 && pattern2 ]]

Both pattern1 and pattern2 are true.

[[ pattern1 || pattern2 ]]

Either pattern1 or pattern2 is true.

[[ ! pattern ]]

Not a pattern match.

Integer Test

[ int1 –eq int2 ]

Int1 is equal to int2.

[ int1 –ne int2 ]

Int1 is not equal to int2.

[ int1 –gt int2 ]

Int1 is greater than int2.

[ int1 –ge int2 ]

Int1 is greater than or equal to int2.

[ int1 –lt int2 ]

Int1 is less than int2.

[ int1 –le int2 ]

Int1 is less than or equal to int2.

Binary Operators for File Testing

[ file1 –nt file2 ]

True if file1 is newer than file2 (according to modification date).

[ file1 –ot file2 ]

True if file1 is older than file2.

[ file1 –ef file2 ]

True if file1and file2 have the same device or inode numbers.

 

shopt 用来设置一些shell的属性

 

[[ $name == [Tt]o+(m)y ]]
start with T/t, followed 1 or n m, end with y.

 

 test -n -----not-null

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值