1

我正在尝试为员工计算 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 
4

1 回答 1

2

BASH 只支持整数运算,不支持浮点运算。使用bc -lawk

例子:

bc -l <<< "1.5 * 20"
30.0
于 2013-11-07T16:03:36.543 回答