0

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.

johnthor
  • 1
  • 5
  • 2
    In ```if [[ `"$hours"` -le 40 ]]``` The backticks around `"$hours"` means to execute its value as a command. That makes no sense. What are you trying to do there? – Barmar Nov 18 '22 at 00:46
  • when i dont use them it gives me another error like "30.20: syntax error: invalid arithmetic operator (error token is ".20") AND the math ends up wrong for some reason. – johnthor Nov 18 '22 at 00:48
  • 1
    That's because `test` doesn't support floating point numbers. Why would you expect that backticks would fix that? – Barmar Nov 18 '22 at 00:50
  • 1
    See https://stackoverflow.com/questions/8654051/how-can-i-compare-two-floating-point-numbers-in-bash – Barmar Nov 18 '22 at 00:51
  • Put this code, with a `#!/bin/bash` in https://www.shellcheck.net/ to see even more things to fix. – Nic3500 Nov 18 '22 at 00:53
  • I don't know what I'm doing. My professor shouldn't be teaching and doesn't explain anything and makes countless mistakes in his codes and its all we have to go off. – johnthor Nov 18 '22 at 00:56
  • The backtick expression produces an empty string after the error, which in this context is interpreted as 0. No matter *what* number of hours you entered, the comparison would succeed, so you wouldn't get the correct result if you entered, say, 41 hours. – chepner Nov 18 '22 at 01:58
  • 1
    Blaming your teacher when you yourself can't tell the difference between correct and incorrect code is not a good look. (Maybe he *can't* teach, but that's irrelevant to your question and isn't going to generate you much sympathy.) – chepner Nov 18 '22 at 01:59
  • how am i supposed to tell the difference between correct and incorrect code if im being taught incorrectly? i came here for help, not to be ridiculed. – johnthor Nov 19 '22 at 20:12

1 Answers1

0

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!

Eric Marceau
  • 1,601
  • 1
  • 8
  • 11
  • Thanks! I was able to get it working. I understand what its doing, but dont know the syntax too well because of our course material. Seems very outdated. If yall saw my professors actual code for the other labs we did, yall would really flip out. – johnthor Nov 22 '22 at 22:01
  • The double square-brackets are used as a replacement for the "test" or "if" command. – Eric Marceau Nov 23 '22 at 02:38