格式:test 测试条件
字符串测试:注意空格:test str1 == str2 测试字符串是否相等test str1 != str2 测试字符串是否不相等test str1 测试字符串是否不为空test -n str1 测试字符串是否不为空test -z str1 测试字符串是否为空整数测试test int1 -eq int2 测试整数是否相等test int1 -ge int2 测试int1是否>=int2test int1 -gt int2 测试int1是否>int2test int1 -le int2 测试int1是否<=int2test int1 -lt int2 测试int1是否
测试语句一般不单独使用,一般作为if语句的测试条件,如:
if test "hello" == "hello" ;thencommands....fi上面语句也可简化为(注意[]与"之间的空格)if [ "hello" == "hello" ];then....
看一段代码:
#!/bin/bashif test "hello" == "hello" ;thenecho "equals"elseecho "not equals"fiif test -z "" ;thenecho "str is null"fiif test -n "" ;thenecho "str is not null"fiif test "9" ;thenecho "not null"elseecho "null"fi#easy wayif [ "hello" == "hello" ];thenecho "equals"elseecho "not equals"fiif [ -f /root/test/test1 ];thenecho "test1 is a file"elif [ -d /root/test/test1 ];thenecho "test1 is a dir"elseecho "i don't know the result"fi
执行效果:
版权声明:本文为博主原创文章,未经博主允许不得转载。