整数比较
-eq
等于
if [ "$a" -eq "$b" ]
-ne
不等于
if [ "$a" -ne "$b" ]
-gt
大于if [ "$a" -gt "$b" ]
-ge
大于等于
if [ "$a" -ge "$b" ]
-lt
小于
if [ "$a" -lt "$b" ]
-le
小于等于
if [ "$a" -le "$b" ]
<
小于(在双括号中使用)
(("$a" < "$b"))
<=
小于等于(在双括号中使用)
(("$a" <= "$b"))
>
大于(在双括号中使用)
(("$a" > "$b"))
>=
大于等于(在双括号中使用)
(("$a" >= "$b"))
字符串比较
=
等于
if [ "$a" = "$b" ]
==
等于
if [ "$a" == "$b" ]
与=等价.
==比较操作符在双中括号对和单中括号对中的行为是不同的. [[ $a == z* ]] # 如果$a以"z"开头(模式匹配)那么结果将为真 [[ $a == "z*" ]] # 如果$a与z*相等(就是字面意思完全一样), 那么结果为真. [ $a == z* ] # 文件扩展匹配(file globbing)和单词分割有效. [ "$a" == "z*" ] # 如果$a与z*相等(就是字面意思完全一样), 那么结果为真.
!=
不等号
if [ "$a" != "$b" ]
这个操作符将在[[ ... ]]结构中使用模式匹配.
<
小于, 按照ASCII字符进行排序
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ]
注意"<"使用在[ ]结构中的时候需要被转义.
>
大于, 按照ASCII字符进行排序
if [[ "$a" > "$b" ]]
if [ "$a" \> "$b" ]
注意">"使用在[ ]结构中的时候需要被转义.
-z
字符串为"null", 意思就是字符串长度为零
-n
字符串不为"null".
当-n使用在中括号中进行条件测试的时候, 必须要把字符串用双引号引用起来. 如果采用了未引用的字符串来使用! -z, 甚至是在条件测试中括号(参见例子 7-6)中只使用未引用的字符串的话, 一般也是可以工作的, 然而, 这是一种不安全的习惯. 习惯于使用引用的测试字符串才是正路
ex:算术比较与字符串比较
#!/bin/bash
a=4
b=5
echo
if [ "$a" -ne "$b" ]
then
echo "$a is not equal to $b."
echo "(string comparison)"
fi
echo
exit 0
执行结果:
4 is not equal to 5.
(string comparison)
ex:检查字符串是否为空
#!/bin/bash
# str-test.sh
if [ -n $string1 ]
then
echo "String \"string1\" is not null."
else
echo "String \"string1\" is null."
fi
echo
if [ -n "$string1" ]
then
echo "String \"string1\" is not null."
else
echo "String \"string1\" is null."
fi
echo
if [ $string1 ]
then
echo "String \"string1\" is not null."
else
echo "String \"string1\" is null."
fi
echo
string1=initialized
if [ $string1 ]
then
echo "String \"string1\" is not null."
else
echo "String \"string1\" is null."
fi
echo
string1="a=b"
if [ $string1 ]
then
echo "String \"string1\" is not null."
else
echo "String \"string1\" is null."
fi
exit 0
执行结果:
String "string1" is not null.
String "string1" is null.
String "string1" is null.
String "string1" is not null.
String "string1" is not null.
结论:
1.[ -n $string1 ] 结果错误,显示为非空
2.[ -n "$string1" ] 结果正确。注意引用的字符必须放入中括号中
3.[ $string1 ] 结果正确。[ ]测试操作符能够独立检查string是否为空,但是使用"$string1"是一种非常好的习惯
4.结果正确[ $string1 ] 只有一个参数"]"
[ "$string1" ] 有两个参数 空的"$string1" 和 "]"
5.结果正确,但书中提示结果是错误的,[待考究]
ex:zmore(使用‘cat’来查看shell文件)
#!/bin/bash
# zmore.sh
NOARGS=65
NOTFOUND=66
NOTGZIP=67
NOTSHELL=68
if [ $# -eq 0 ]
then
echo "Usage:`basename $0` filename" >&2
# 错误信息输出到stderr
exit $NOARGS
#返回65作为退出脚本的状态码
fi
filename=$1
if [ ! -f "$filename" ]
then
echo "File $filename not found!" >&2
exit $NOTFOUND
fi
if [ "${filename##*.}" != "sh" ]
then
echo "Filename $1 is not a shell file!"
exit $NOTSHELL
fi
cat $1 | more
exit $?
# 脚本将把管道的退出状态作为返回值
# 任何情况下脚本都会将最后一条命令的退出状态作为返回值
执行结果:
$ sh zmore.sh zmor
File zmor not found!
$ sh zmore.sh zmore.sh~
Filename zmore.sh~ is not a shell file!
$ sh zmore.sh zmore.sh
#!/bin/bash
# zmore.sh
逻辑判断
-a
逻辑与
exp1 -a exp2 如果表达式exp1和exp2都为真的话, 那么结果为真.
-o
逻辑或
exp1 -o exp2 如果表达式exp1和exp2中至少有一个为真的话, 那么结果为真.
这与Bash中的比较操作符&&和||非常相像, 但是这个两个操作符是用在双中括号结构中的.
[[ condition1 && condition2 ]]
-o和-a操作符一般都是和test命令或者是单中括号结构一起使用的.
if [ "$exp1" -a "$exp2" ]