【发布时间】:2016-02-15 03:22:05
【问题描述】:
我正在学习 bash 脚本,目前在函数中使用 if 语句时遇到问题。以下代码返回两个错误;两者都引用if 和elif 条件并说[: 12000: unary operator expected。
function calculateBonus {
# If the sales figure is greater than 1 million, bonus is £1500
if [ $1 >= 1000000 ]
then
bonus=1500
# If greater than 100000, bonus is £750
elif [ $1 >= 100000 ]
then
bonus=750
else
bonus=0
fi
# Return the bonus figure
return $bonus
}
read sales
bonus=$(calculateBonus $sales)
我尝试过使用双方括号,但由于某种原因出现语法错误。当我使用[[ some_condition ]] 而不是[ some_condition ] 时,有人可以解释上述错误的原因以及语法错误。
【问题讨论】:
-
IIRC 更大相等的运算符不是
>=,而是-ge。查看man test(test应该和[一样)
标签: bash