Condition test
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 “*”