Home > database >  Linux bash scripting command not found for a float input
Linux bash scripting command not found for a float input

Time:11-23

I have a lab due for school and the professor coded it for us in class today, but he always leaves errors (not to test us, but rather because he shouldn't be teaching/coding at all)

i have the correct output but in my output is a command not found error for some reason.

# 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

and my output copied and pasted:

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

i force close the app after that because i know the math is working, even for the overtime cases. can anyone explain why this error is coming up? we don't use shebangs; he never taught us to use them and again he really really really should not be a programming professor.

CodePudding user response:

You have incorrectly used double square brackets. You only need one set for each of the while and if statements (that applies to sh or bash). Also, strongly recommend to wrap you variables with braces (i.e. ${variables} ).

As barmar said, the backquotes don't apply in the if statement. The ${hours} by itself will instantiate the value for the if test.

With those changes, the script session went as follows:

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$

As a critical constructive suggestion: NEVER run code if you don't truly know what it is doing!

For that reason, if the teacher is not helpful, you need to find a good book on bash, and try reading the first half before attempting much coding. That will give you a sense of the animal you are trying to tame before letting it loose among your chickens. Otherwise, you may wind up with a disk full of dead chickens!

  • Related