从一个问题引发
最近我们项目碰到一个regression bug究其原因是因为对bash语言语句的理解不深刻。我们有一个脚本接收一个数字参数,指定操作的类型。
#!/bin/bash
if [ $1 -eq 1 ]; then
echo "do_something_1"
else
echo "do_other"
fi
后来随着参数类型越来越多,使用数字不是很方便描述,就准备增强脚本使用有意义的字符串参数来表达参数类型:
#!/bin/bash
if [ $1 = "read" ]; then
echo "do_read"
elif [ $1 = "write" ]; then
echo "do_write"
else
echo "do_other"
fi
这两个脚本本身都没有问题;但是考虑到项目的实际情况,很多原来的调用脚本已经使用数字作为参数了,就不想去改那些脚本,就考虑把被调用脚本增强成兼容数字和字符串参数:
#!/bin/bash
if [ $1 -eq 1 -o $1 = "read" ]; then
echo "do_read"
elif [ $1 -eq 2 -o $1 = "write" ]; then
echo "do_write"
else
echo "do_other"
fi
这个脚本看起来逻辑上是通的,但实际上是不正确的,看测试例子:
$ ./test.sh 1
do_read
$ ./test.sh "read"
./test.sh: line 3: [: read: integer expression expected
./test.sh: line 5: [: read: integer expression expected
do_other
当传入参数是数字的时候,没有问题,当出入字符串的时候,我们看到bash希望报错,提示希望输入一个数字。
这根源上说属于弱类型的解释型语言无法避免的错误,如果是强类型的编译型语言,这个问题就能在编译时刻被发现,不至于在运行时刻才暴露出问题。
解决办法
我们知道eq的比较是数字类型比较,如果我们把数字也按字符串来比较,问题就能轻易避免
#!/bin/bash
if [ $1 = 1 -o $1 = "read" ]; then
echo "do_read"
elif [ $1 = 2 -o $1 = "write" ]; then
echo "do_write"
else
echo "do_other"
fi
在看运行结果
$ ./test.sh 1
do_read
$ ./test.sh read
do_read
$ ./test.sh 2
do_write
$ ./test.sh write
do_write
$ ./test.sh 3
do_other
$ ./test.sh any
do_other
因为在此场景下数字1,2,3都是按字符串"1","2","3"类进行比较的,也就是说调用者传入的都是字符串。
附件: bash的if语句语法
if [ conditional-expression1 ]; then
statement...
elif [ conditional-expression2 ]; then
statement...
else
statement...
fi
注意关键字then可以换行起,但我个人还是喜欢放在同一行。
条件表达式 conditional-expression
-n STRING The length of STRING is greater than zero.
-z STRING The lengh of STRING is zero (ie it is empty).
STRING1 = STRING2 STRING1 is equal to STRING2
STRING1 != STRING2 STRING1 is not equal to STRING2
INTEGER1 -eq INTEGER2 INTEGER1 is numerically equal to INTEGER2
INTEGER1 -gt INTEGER2 INTEGER1 is numerically greater than INTEGER2
INTEGER1 -lt INTEGER2 INTEGER1 is numerically less than INTEGER2
-d FILE FILE exists and is a directory.
-e FILE FILE exists.
-r FILE FILE exists and the read permission is granted.
-s FILE FILE exists and it's size is greater than zero (ie. it is not empty).
-w FILE FILE exists and the write permission is granted.
-x FILE FILE exists and the execute permission is granted.
条件表达式的逻辑运算
与或非操作
[ cond-exper1 -a cond-exper2] # and
[ cond-exper1 -o cond-exper2] # or
[ ! cond-exper ] # not
网友评论