未找到用于浮点输入的Linux bash脚本命令



我有一个实验室要交,教授今天在课堂上给我们写了代码,但他总是留下错误(不是为了测试我们,而是因为他根本不应该教/写代码)

我有正确的输出,但在我的输出是一个命令没有发现错误的原因。

# MUST use a loop
# MUST prompt user for input data
# MUST use ecoh for calculation
# Program that computes a payroll for a small company
# 11.17.2022
# Johnathon Masias
# ITSC 1307 FALL
# Scripting Lab 8
# Variable declaration
rate=0
hours=0
gross=0
counter=0
# Using a loop to collect data
while [[ $counter -lt 3 ]]
do
echo "Please enter employee name"
read name
echo "Please enter hours worked"
read hours
echo "Please enter hourly rate"
read rate
# Compute gross pay using IF statement for overtime
if [[ `"$hours"` -le 40 ]]
then
gross=`echo "scale=2; $hours * $rate" | bc`
else
gross=`echo "scale=2; (40 * $rate) + ($hours - 40)*($rate*1.5)" | bc`
fi
echo "$name worked $hours at a rate of $rate making a gross pay of $gross"
read dummy
counter=`expr $counter + 1`
done

和我的输出复制粘贴:

Please enter employee name
a
Please enter hours worked
30.20
Please enter hourly rate
10.25
script08: line 30: 30.20: command not found
a worked 30.20 at a rate of 10.25 making a gross pay of 309.55
在那之后我强制关闭应用程序,因为我知道数学是有效的,即使是加班的情况下。有人能解释一下为什么会出现这个错误吗?我们不用刘海;他从来没有教过我们如何使用它们,再说一次,他真的真的真的不应该成为一个编程教授。

您错误地使用了双方括号。每个while和if语句只需要一组语句(适用于sh或bash)。此外,强烈建议用大括号(即${variables})包装变量。

正如barmar所说,反引号在if语句中不适用。${hours}本身将实例化if测试的值。

修改后,脚本会话如下:

ericthered@OasisMega1:/0__WORK$ ./test_48.sh
Please enter employee name
abc
Please enter hours worked
55
Please enter hourly rate
20
abc worked 55 at a rate of 20 making a gross pay of 1250.0
^C
ericthered@OasisMega1:/0__WORK$

作为一个重要的建设性建议:NEVER如果你真的不知道代码在做什么,就运行它吧!

因此,如果老师没有帮助,您需要找到一本关于bash的好书,并在尝试编写更多代码之前尝试阅读前半部分。这样在你把它放到鸡群里之前,你就能对你要驯服的动物有个概念。否则,你可能会得到一盘死鸡!

最新更新