首页 > 科技 > Condition test

Condition test

2006年5月10日 17点12分 发表评论 阅读评论

test: is used for testing string, file and number.
expr: command testing and output the result

test
1.file testing: # test condition
or # [ condition ] (the are two space characters in each side of “condition” strings)
file testing have the following condition expressions:
-d: directory, to check if the directory exists or not
-f: general file
-L:this “L” is upcase,means link.
-r: readable
-w: is able to write
-x: is able to execute
-s: check the files length is bigger than 0, and NOT Null
-u: if there is suid been set.

logical expression:
-a: AND
-o: OR
!: NOT
for example: # [ -w test1.txt -a -w test2.txt ]
Pay attention here, # [ -w test1.txt ] -a [ -w test2.txt ] is wrong because only one pair [] could be used.

2.string testing
syntax: # test “string”
# test string_operator “string”
# test “string” string operator “string”
# [ string_operator string ]
# [ “string” string_operator “string”]
these string operators would be: ” = ” ,” != ” , “-z”, “-n”
-z: NULL
-n: NOT NULL
for example, to see if the EDITOR was NULL or not:
# [-z $EDITOR]
# echo $?
Is the EDITOR “vi”?
#[ $EDITOR = “vi” ]
# echo $?

# [ $TEST1 = $TEST2 ]
# echo $?

# [ $TEST1 != $TEST2 ]
# echo $?

3.numeric testing
syntax: # test “number” numeric_operator “number”
or # [ “number” numeric_operator “number” ]
The following operator are used here:
-eq: ==
-ne: !=
-gt:>
-lt:< -ge:>=

Expr: evaluation expressions
syntax: expr argument operator argument
Note,there must be two space characters in each side of “operator”, and when using “*”, the back slash should be added as “*”

分类: 科技 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
您必须在 登录 后才能发布评论.