很多shell运算需要获取环境变量,例如a+b,其中a或者b可能都来自于环境变量。但是环境变量可能出现不存在的情况,这时运算就会出错。
所以在计算前需要判断其是否存在
[ -z $test ]
更复杂的
i
nput=$1
test_mode=$2
if [ -z $test_mode ]; then
ret=1
elif [ $test_mode -eq 1234 ] && [$input -eq 5678 ];then
ret=1
else
ret=0
fi
echo $ret
测试结果
[zl@localhost test]$ ./test.sh
1
[zl@localhost test]$ ./test.sh 1234
1
[zl@localhost test]$ ./test.sh 1234 5678
0
[zl@localhost test]$