变量测试语句 变量测试语句:用于测试变量是否相等、 是否为空、文件类型等。 格式 : test 测试条件 测试范围 : 整数、字符串、文件 *1 字符串测试: test str1=str2 测试字符串是否相等 test str1!=str2 测试字符串是否不相等 test str1 测试字符串是否不为空 test -n str1 测试字符串是否不为空 test -z str1 测试字符串是否为空 *2 整数测试: test int1 -eq int2 测试整数是否相等 test int1 -ge int2 测试int1是否>=int2 test int1 -gt int2 测试int1是否>int2 test int1 -le int2 测试int1是否<=int2 test int1 -lt int2 测试int1是否 test int1 -ne int2 测试整数是否不相等 *3 文件测试: test -d file 指定文件是否目录 test -f file 指定文件是否常规文件 test -x file 指定文件是否可执行 test -r file 指定文件是否可读 test -w file 指定文件是否可写 test -a file 指定文件是否存在 test -s file 文件的大小是否非0 变量测试语句一般不单独使用,一般做为if语句的测试条件,如: if test -d $1 then ... fi 变量测试语句可用[]进行简化,如 test -d $1 等价于 [ -d $1 ]