随笔 - 56, 文章 - 0, 评论 - 0, 引用 - 0
数据加载中……

《LInux与Unix Shell编程指南》笔记之条件测试

                                             条件测试
                                                点击打开本章pdf文档
    有时判断字符串是否相等或检查文件状态或是数字测试等。Test命令用于测试字符串,文件状态和数字。
    测试文件状态
    格式
    test condition
    或 [ condition ]
    使用方括号时,要注意在条件两边加上空格。
    常用的条件表达式为:
                    文件测试状态
    -----------------------------------------------------------------------
   -d        目录                  -s        文件长度大于0、非空
   -f         正规文件          -w       可写
   -L        符号链接          -u        文件有suid位设置
   -r         可读                  -x        可执行
   -z         字符串为空
    -----------------------------------------------------------------------
    测试的结果可以用$?的值来判断,0表示成功,其他为失败。
    下面测试文件xaa是否可写:
    [root@localhost test]# ls -l
    
-rw-r--r-- 1 root root      28 02-23 12:44 xaa
    
-rw-r--r-- 1 root root      28 02-23 12:44 xab
    
-rw-r--r-- 1 root root      28 02-23 12:44 xac
    [root@localhost test]# [ 
-w xaa ]
    [root@localhost test]# echo $
?
    
0
    证明是可写的.下面测试是否可运行:
    [root@localhost test]# [ -x xaa ]
    [root@localhost test]# echo $
?
    
1
    要测试其他的类似.
    
    测试时使用逻辑操作符
    -a         逻辑与,操作符两边均为真,结果为真,否则为假。
    -o         逻辑或,操作符两边一边为真,结果为真,否则为假。
    !           逻辑否,条件为假,结果为真。
        下面的例子测试两个文件xaa和xab是否均可读.
    [root@localhost test]# [ -w xab --w xac ]
    [root@localhost test]# echo $
?
    
0
    测试xac是否可执行或者xab是否可写,使用逻辑或操作.
    [root@localhost test]# [ -w xab --x xac ]
    [root@localhost test]# echo $
?
    
0
    测试xac是否可执行而且xab是否可写,使用逻辑或操作.
    [root@localhost test]# [ -w xab --x xac ]
    [root@localhost test]# echo $
?
    
1
    
    字符串测试
    5种格式:
    test "string"
    test string_operator 
"string"
    test 
"string" string_operator "string"
    [ string_operator 
string ]
    [ 
string string_operator string ]
    string_operator可为:
    =    两个字符串相等
    !=    两个字符串不等
    -z    空串
    -n    非空串
    例如:
    测试环境变量EDITOR是否为空:
    [root@localhost test]# echo $EDITOR

    [root@localhost test]# [ 
-z $EDITOR ]
    [root@localhost test]# echo $
?
    
0
    返回为真,所以EDITOR是空.
    修改EDITOR:
    [root@localhost test]# EDITOR="vi"
    [root@localhost test]# echo $EDITOR
    vi
    [root@localhost test]# [ 
-z $EDITOR ]
    [root@localhost test]# echo $
?
    
1
    测试两个字符串:
    [root@localhost test]# AAA="aaa"
    [root@localhost test]# BBB
="bbb"
    [root@localhost test]# AAA1
="aaa"
    [root@localhost test]# [ 
"$AAA" = "$BBB" ]
    [root@localhost test]# echo $
?
    
1
    [root@localhost test]# [ 
"$AAA" = "$AAA" ]
    [root@localhost test]# echo $
?
    
0
    
    测试两个数值的大小:
    格式:
    "number" numberic_operator "number"
    或者
    [ "number" numberic_operator "number" ]
    numberic_operator可为:
    -eq         数值相等。
    -ne         数值不相等。
    -gt         第一个数大于第二个数。
    -lt          第一个数小于第二个数。
    -le         第一个数小于等于第二个数。
    -ge         第一个数大于等于第二个数。
    例如:
    [root@localhost test]# NUMBER=130
    [root@localhost test]# [ 
"$NUMBER" -eq "130" ]
    [root@localhost test]# echo $
?
    
0
    
    expr用法
    格式:
    expr argument operator argument
    可以作为手工计数器:
    [root@localhost test]# expr 10 + 10
    
20
    [root@localhost test]# expr 
30/3
    
30/3
    [root@localhost test]# expr 
30 / 3
    
10
    [root@localhost test]# expr 
30 / 3 / 2
    
5
    [root@localhost test]# expr 
30 \* 3
    
90
    注意:operator左右的数字要有空格:
    如:
    [root@localhost test]# expr 30/3
    
30/3
    没有达到预期效果:而且使用乘号时也要用反斜线屏蔽其特殊意义.
   
    expr还可用于锃亮计算,如一个循环体需要循环次数:
    LOOP=0
    LOOP
=`expr $LOOP + 1`
    
    数值测试
    VALUE_1=12
    VALUE_2
=13
    expr $VALUE_1 
> $VALUE_2
    echo $
?
    
1
    字符串测试:
    [root@localhost test]# VALUE="hello"
    [root@localhost test]# expr $VALUE
="hello"
    hello
=hello
    [root@localhost test]# expr $VALUE 
= "hello"
    
1
    [root@localhost test]# echo $
?
    
0
    有两点注意:
    一是$VALUE = "hello"的等号左右的字符串要有空格,没有空格没有达到预期结果,如上所示;
    二是用expr测试的返回结果真为1,假为0.与$?的结果相反.
   
    模式匹配
    使用expr通过指定冒号选项计算字符串中字符数 .".*"意即任何字符重复0次或多次.
    [root@localhost test]# VALUE=accounts.doc
    [root@localhost test]# expr $VALUE : 
'.*'
    
12
    在expr中可以使用字符串匹配操作,这里使用模式 .doc抽取文件附属名。
    [root@localhost test]# expr $VALUE : '\(.*\).doc'
    accounts
 

posted on 2011-02-26 09:58 八路 阅读(389) 评论(0)  编辑 收藏 引用 所属分类: 嵌入式linux笔记


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理