我正在學習bash腳本,並且當前在函數內部有一個if
語句。以下代碼返回兩個錯誤;兩者都參考if
和elif
條件並且說[: 12000: unary operator expected
。Bash函數:一元運算符預期錯誤
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'應該和'''相同' –