条件测试
test 命令
一.测试文件
一般形式:
1. test condition
2. [ condition ]
文件状态 ,例如 test –d name 或者 [ -d name ]
-d | 目录 | -s | 文件长度大于 0 ,非空 |
-f | 正规文件 | -w | 可写 |
-L | 符号链接 | -u | 文件有 suid 位设置 |
-r | 可读 | -x | 可执行 |
$ test –d name
$ $?
$ 0
0 表示成功, 1 表示返回错误
$? 退出最后状态命令,用来检查测试结果;
二.测试时使用的逻辑符号
-a 逻辑与
-o 逻辑或
!逻辑否
例如
$ [ -w name1 –a –w name2 ]
$ $?
$ 0
三.测试字符串
一般形式:
test “string”
test string_operator “string”
test “string” string_operator “string”
[string_operator string ]
[string string_operator string]
string_operator :
= | 两个字符串相等 | -z | 空串 |
!= | 两个字符串不相等 | -n | 非空串 |
四.测试数值
一般形式
“number” mumeric_operator “number”
[ “number” mumeric_operator “number” ]
mumeric_operator :
-eq | 数值相等 | -lt | 第一个数小于第二个 |
-ne | 数值不相等 | -le | 第一个数小于等于第二个 |
-gt | 第一个数大于第二个 | -ge | 第一个数大于等于第二个 |
例如
$ a=130
$ b=120
$ [ “$a” –lt “$b” ]
$ echo $?
$ 1
可以使用逻辑操作符
如:
$ [ “990” –le “995” ] –a [ “123” –ge “124” ]
$ echo $?
$ 1
五. expr 命令测试执行和执行数值输出
一般用于整数值或者字符串
一般形式
expr argument operator argument
expr 也是手工命令行计数器
$ expr 10 + 10
20
减法 – 除法 / 乘法 /*
1. 增量技术
expr 用于增量计算
$ LOOP=0
$ LOOP=`expr $LOOP + 1`
2. $ value=100
$ expr $value + 10 >/dev/null 2>&1
$ echo $?
0
表示是个数值
$ value=hello
$ expr $value + 10 >/dev/null 2>&1
$ echo $?
3
表示是个非数值符号
3. 模式匹配
字符串匹配操作
$ expr $calue : `/(.*/).txt`
Hdisk.txt
在shell中的应用 循环计数
#!/bin/sh
counter=0
for files in *
do
counter=`expr $counter + 1`
done
echo "There are $counter files in `pwd` we need to process"
整数比较
-eq
等于
if [ "$a" -eq "$b" ]
-ne
不等于
if [ "$a" -ne "$b" ]
-gt
大于
if [ "$a" -gt "$b" ]
-ge
大于等于
if [ "$a" -ge "$b" ]
-lt
小于
if [ "$a" -lt "$b" ]
-le
小于等于
if [ "$a" -le "$b" ]
<
小于(需要双括号)
(("$a" < "$b"))
<=
小于等于(需要双括号)
(("$a" <= "$b"))
> 大于
(需要双括号)
(("$a" > "$b"))
>=
大于等于(需要双括号)
(("$a" >= "$b"))
字符串比较
=
等于
if [ "$a" = "$b" ]
==
等于
if [ "$a" == "$b" ],与=等价
注意:==的功能在[[]]和[]中的行为是不同的,如下:
[[ $a == z* ]]
# 如果$a 以"z"开头(模式匹配)那么将为true
[[ $a == "z*" ]]
# 如果$a 等于z*(字符匹配),那么结果为true
[ $a == z* ]
# File globbing 和word splitting 将会发生
[ "$a" == "z*" ]
# 如果$a 等于z*(字符匹配),那么结果为true
!=
不等于
if [ "$a" != "$b" ]
<
小于,在ASCII 字母顺序下.如:if [[ "$a" < "$b" ]]if [ "$a" /< "$b" ]
注意:在[]结构中"<"需要被转义.
>
大于,在ASCII 字母顺序下.如:if [[ "$a" > "$b" ]]if [ "$a" /> "$b" ]
注意:在[]结构中">"需要被转义.
-z
字符串为"null".就是长度为0.
-n
字符串不为"null"
For Mathematics, use following operator in Shell Script
Mathematical Operator in Shell Script | Meaning | Normal Arithmetical/ Mathematical Statements | But in Shell | |
|
|
| For test statement with if command | For [ expr ] statement with if command |
-eq | is equal to | 5 == 6 | if test 5 -eq 6 | if [ 5 -eq 6 ] |
-ne | is not equal to | 5 != 6 | if test 5 -ne 6 | if [ 5 -ne 6 ] |
-lt | is less than | 5 < 6 | if test 5 -lt 6 | if [ 5 -lt 6 ] |
-le | is less than or equal to | 5 <= 6 | if test 5 -le 6 | if [ 5 -le 6 ] |
-gt | is greater than | 5 > 6 | if test 5 -gt 6 | if [ 5 -gt 6 ] |
-ge | is greater than or equal to | 5 >= 6 | if test 5 -ge 6 | if [ 5 -ge 6 ] |
For string Comparisons use
Operator | Meaning |
string1 = string2 | string1 is equal to string2 |
string1 != string2 | string1 is NOT equal to string2 |
string1 | string1 is NOT NULL or not defined |
-n string1 | string1 is NOT NULL and does exist |
-z string1 | string1 is NULL and does exist |
Shell also test for file and directory types
Test | Meaning |
-s file | Non empty file |
-f file | Is File exist or normal file and not a directory |
-d dir | Is Directory exist and not a file |
-w file | Is writeable file |
-r file | Is read-only file |
-x file | Is file is executable |
Logical Operators
Logical operators are used to combine two or more condition at a time
Operator | Meaning |
! expression | Logical NOT |
expression1 -a expression2 | Logical AND |
expression1 -o expression2 | Logical OR |