我正在尝试为员工计算1.5加班费,但语法错误“无效算术运算符”。我是否需要指示任何特殊的十进制计算命令? 感谢。
错误:
Enter employee name: Mary
Mary is Hourly employee.
Enter hourly wage:1
Enter hours worked this week:42
./Assignment2.sh: line 196: let: hwages = (40 * hsalary) + (overtime * 1.5 * hsalary): syntax error: invalid arithmetic operator (error token is ".5 * hsalary)")
Gross wages: $
Hit [Enter] to return to main menu...
我的代码:
elif [ "$EmployeeType" = "Hourly" ]
then
echo -en "Enter hourly wage:"
read hsalary
echo -en "Enter hours worked this week:"
read hours
if [ "$hours" > 40 ]
then
let "overtime = hours - 40"
let "hwages = (40 * hsalary) + (overtime * 1.5 * hsalary)"
else
let "hwages = hsalary * hours"
fi
echo -en "Gross wages: \$$hwages"
echo
答案 0 :(得分:2)
BASH仅支持整数运算,不支持浮点运算。使用bc -l
或awk
示例:
bc -l <<< "1.5 * 20"
30.0