使用test命令进行进行条件测试
2011-09-14 20:07
387 查看
使用test命令进行进行条件测试
格式: test conditions
test在以下四种情况下使用:
a. 字符比较
b.两个整数值的比较
c. 文件操作,如文件是否存在及文件的状态等
d. 逻辑操作,可以进行and/or,与其他条件联合使用
a. 测试字符数据: shell变量通常均作为字符变量
str1 = str2 二者相长,相同
str1 != str2 不同
-n string string不为空(长度不为零)
-z string string为空
string string不为空
例:
$ str1=abcd #在含有空格时必须用引号括起来
$ test $str1=abcd
$ echo $?
0 #无错
$ str1="abcd "
$ test $str1=abcd
$ echo $?
1 #有错,因为str1的字符串后面还有个空格,不等于abcd
Note: 在test处理含有空格的变量时最好用引号将变量括起来,否则会出现错误的
结果,
因为shell在处理命令行时将会去掉多余的空格,而用引号括起来则可以防止
shell去掉这些空格.
例:
$ str1=" "
$ test $str1
$ echo $?
1
$ test "$str1"
$ echo $?
0
$ test -n $str1
test: argument expected
$ test -n "$str1"
$ echo $?
0
$
b. 整数测试: test与expr相同,可以将字符型变量转换为整数进行操作,expr进行
整数的算术运算,而test则进行逻辑运算.
表达式 说明
---------------------------------------
int1 -eq int2 相等?
int1 -ne int2 不等?
int1 -gt int2 int1 > int2 ?
int1 -ge int2 int1 >= int2 ?
int1 -lt int2 int1 < int2 ?
int1 -le int2 int1 <= int2 ?
例:
$ int1=1234
$ int2=01234
$ test $int1 -eq $int2
$ echo $?
0
c. 文件测试:检查文件状态如存在及读写权限等
-r filename 用户对文件filename有读权限?
-w filename 用户对文件filename有写权限?
-x filename 用户对文件filename有可执行权限?
-f filename 文件filename为普通文件?
-d filename 文件filename为目录?
-c filename 文件filename为字符设备文件?
-b filename 文件filename为块设备文件?
-s filename 文件filename大小不为零?
-t fnumb 与文件描述符fnumb(默认值为1)相关的设备是一个终端设备?
d. 测试条件之否定,使用!
例:
$ cat /dev/null > empty
$ test -r empty
$ echo $?
0
$ test -s empty
1
$ test ! -s empty
$ echo $?
0
e. 测试条件之逻辑运算
-a And
-o Or
例: $ test -r empty -a -s empty
$ echo $?
1
f. 进行test测试的标准方法
因为test命令在 shell编程中占有很重要的地位,为了使shell能同其他编程语言
一样
便于阅读和组织, Bourne Shell在使用test测试时使用了另一种方法:用方括号将
整个
test测试括起来:
$ int1=4
$ [ $int1 -gt 2 ]
$ echo $?
0
格式: test conditions
test在以下四种情况下使用:
a. 字符比较
b.两个整数值的比较
c. 文件操作,如文件是否存在及文件的状态等
d. 逻辑操作,可以进行and/or,与其他条件联合使用
a. 测试字符数据: shell变量通常均作为字符变量
str1 = str2 二者相长,相同
str1 != str2 不同
-n string string不为空(长度不为零)
-z string string为空
string string不为空
例:
$ str1=abcd #在含有空格时必须用引号括起来
$ test $str1=abcd
$ echo $?
0 #无错
$ str1="abcd "
$ test $str1=abcd
$ echo $?
1 #有错,因为str1的字符串后面还有个空格,不等于abcd
Note: 在test处理含有空格的变量时最好用引号将变量括起来,否则会出现错误的
结果,
因为shell在处理命令行时将会去掉多余的空格,而用引号括起来则可以防止
shell去掉这些空格.
例:
$ str1=" "
$ test $str1
$ echo $?
1
$ test "$str1"
$ echo $?
0
$ test -n $str1
test: argument expected
$ test -n "$str1"
$ echo $?
0
$
b. 整数测试: test与expr相同,可以将字符型变量转换为整数进行操作,expr进行
整数的算术运算,而test则进行逻辑运算.
表达式 说明
---------------------------------------
int1 -eq int2 相等?
int1 -ne int2 不等?
int1 -gt int2 int1 > int2 ?
int1 -ge int2 int1 >= int2 ?
int1 -lt int2 int1 < int2 ?
int1 -le int2 int1 <= int2 ?
例:
$ int1=1234
$ int2=01234
$ test $int1 -eq $int2
$ echo $?
0
c. 文件测试:检查文件状态如存在及读写权限等
-r filename 用户对文件filename有读权限?
-w filename 用户对文件filename有写权限?
-x filename 用户对文件filename有可执行权限?
-f filename 文件filename为普通文件?
-d filename 文件filename为目录?
-c filename 文件filename为字符设备文件?
-b filename 文件filename为块设备文件?
-s filename 文件filename大小不为零?
-t fnumb 与文件描述符fnumb(默认值为1)相关的设备是一个终端设备?
d. 测试条件之否定,使用!
例:
$ cat /dev/null > empty
$ test -r empty
$ echo $?
0
$ test -s empty
1
$ test ! -s empty
$ echo $?
0
e. 测试条件之逻辑运算
-a And
-o Or
例: $ test -r empty -a -s empty
$ echo $?
1
f. 进行test测试的标准方法
因为test命令在 shell编程中占有很重要的地位,为了使shell能同其他编程语言
一样
便于阅读和组织, Bourne Shell在使用test测试时使用了另一种方法:用方括号将
整个
test测试括起来:
$ int1=4
$ [ $int1 -gt 2 ]
$ echo $?
0
相关文章推荐
- test命令进行条件测试
- linux [] 与 test 命令 进行 条件判断
- 使用Android Test Project进行代码测试
- 使用Android Studio进行UI测试与Record Espresso Test自动生成测试代码
- 使用ACT(Microsoft Application Center Test) 进行压力测试
- 如何使用Coded UI Test对Webpage进行自动化测试
- [原创]使用QuickTest Pro 进行测试流程
- MSDN Webcast 预告:使用Windows Mobile Test Framework进行自动化测试
- 使用 py.test 对 python 代码进行测试
- 使用Windows Mobile Test Framework进行Windows Mobile程序的自动化测试
- 在windows下使用cmd命令通过adb shell自动调用android模拟或真机的InstrumentRunner进行android 自动化测试
- Linux下使用ab命令进行高并发压力测试
- 使用pytest+requests进行接口测试实践——测试get请求
- 使用shell进行调用脚本或者命令driver的测试
- 在使用Microsoft Visual Studio Test Controller 2010进行测试时,报如下错误: System.DllNotFoundException: 无法加载 DLL“Microsoft.VisualStudio.QualityTools.RecorderBarBHO100.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。
- 如何使用Coded UI Test对Webpage进行自动化测试
- Mybatis集成Spring MVC,使用Spring test进行测试
- 使用 TestApi 进行错误注入测试 (msdn)
- 关于flask 上直接使用py.test测试框架进行测试
- maven中使用spring的test包结合junit4进行测试。