1.字符串测试
| 表达式 | 含义 |
|---|---|
| s1=s2 | 判断两个字符串的内容是否相同 |
| s1!=s2 | 判断两个字符串的内容是否不相同 |
| -z s | 判断字符串s的长度是否为零 |
| -n s | 判断字符串s的长度是否不为零 |
注意:=和!= 两边有留有至少一个空格否则就是赋值语句,永远为真。
实例:
test.sh
#!/bin/bash
s1="hello"
s2="hello"
s3="Hello"
test $s1 = $s2
echo "s1==s2=>$?"
test $s1 = $s3
echo "s1==s3=>$?"
test $s1 != $s3
echo "s1!=s3=>$?"
test -z $s1
echo "s1 length is zero:$?"
test -n $s3
echo "s3 length is not zero:$?"
测试运行:
[root@iz2zefozq9h39txdb8s7npz shelldemo]# ./test.sh
s1==s2=>0
s1==s3=>1
s1!=s3=>0
s1 length is zero:1
s3 length is not zero:0
2.数字测试
| 表达式 | 含义 |
|---|---|
| a -eq b | 判断两个数字是否相同 |
| a -ne b | 判断两个数字是否不相同 |
| a -gt b | 判断a是否大于b |
| a -ge b | 判断a是否大于等于b |
| a -lt b | 判断a是否小于b |
| a -le b | 判断a是否小于等于b |
实例:
test.sh
#!/bin/bash
echo "please input num1 and num2:"
read NUM1 NUM2
test $NUM1 -eq $NUM2
echo "num1==num2=>$?"
test $NUM1 -ne $NUM2
echo "num!=num2=>$?"
test $NUM1 -gt $NUM2
echo "num1>num2=>$?"
test $NUM1 -ge $NUM2
echo "num1>=num2=>$?"
test $NUM1 -lt $NUM2
echo "num1<num2=>$?"
test $NUM1 -le $NUM2
echo "num<=num2=>$?"
测试运行:
[root@iz2zefozq9h39txdb8s7npz shelldemo]# ./test.sh
please input num1 and num2:
23 45
num1==num2=>1
num!=num2=>0
num1>num2=>1
num1>=num2=>1
num1<num2=>0
num<=num2=>0