语法
条件测试语法上有两种格式
- test condition
- [ condition ]注意两个中括号和参数之间必须有空格
逻辑运算符
运算符 | 解释 |
---|---|
-a | 逻辑与 |
-o | 逻辑或 |
! | 逻辑与 |
&& | 逻辑与 |
|| | 逻辑或 |
例如:
[ -f temp ] && echo "temp is a file"
[-f temp] || echo "temp is not a file"
- 文本条件测试
例如:
if [ -w temp -a -r temp ] ; then #两个中扩号两边都需要有空格,共计4个空格
echo "temp is reading and writing"
else
echo "temp isn't reading and writing"
fi
- 字符串测试
运算符 | 解释 |
---|---|
= | 两字符串相等 |
!= | 两字符串不相等 |
-z | 空串 zero |
-n | 非空串 nozero |
例如:
temp=1
if [ -z temp ] ; then
echo "temp is null"
else
echo "temp is not null"
fi
- 数值测试
例如:
s1=1
s2=2
if [ $s1 -lt $s2 ] ; then
echo "s1小于s2"
else
echo "s1大于s2"
fi