0

I wrote the following Bash script to do grade calculation (as an assignment):

#!/bin/bash

read -p "Enter assignment mark (0 to 40):" assignment
read -p "Enter Test1 mark (0 to 15):" testo
read -p "Enter Test2 mark (1 to 15):" testa
read -p "Enter Final exam mark (1 to 30):" finalexam

grades = $(($assignment + $testo + $testa + $finalexam))
if [ $grades -ge 0 ] && [ $grades -lt 49 ] 
then
        echo "Your final grade is F"
elif [ $grades -ge 50 ] && [ $grades -lt 52 ]
then
        echo "Your final grade is D-"
elif [ $grades -ge 53 ] && [ $grades -lt 56 ]
then 
        echo "Your final grade is D"
elif [ $grades -ge 57 ] && [ $grades -lt 59 ]
then 
        echo "Your final grade is D+"
elif [ $grades -ge 60 ] && [ $grades -lt 62 ]
then 
        echo "Your final grade is C-"
elif [ $grades -ge 63 ] && [ $grades -lt 66 ]
then 
        echo "Your final grade is C"
elif [ $grades -ge 67 ] && [ $grades -lt 69 ]
then 
        echo "Your final grade is C+"
elif [ $grades -ge 70 ] && [ $grades -lt 72 ]
then 
        echo "Your final grade is B-"
elif [ $grades -ge 73 ] && [ $grades -lt 76 ]
then 
        echo "Your final grade is B"
elif [ $grades -ge 77 ] && [ $grades -lt 79 ]
then 
        echo "Your final grade is B+"
elif [ $grades -ge 80 ] && [ $grades -lt 84 ]
then 
        echo "Your final grade is A-"
elif [ $grades -ge 85 ] && [ $grades -lt 89 ]
then 
        echo "Your final grade is A"
elif [ $grades -ge 90 ] && [ $grades -lt 100 ]
then 
        echo "Your final grade is A+"
else
echo "Wrong input"
fi

When I run this script I get the error

line 8: grades: command not found

I cannot figure out what is wrong. How can I fix it?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Jamal
  • 31
  • 5
  • Here are few things to improve your future questions. When asking for _debugging help_ always include your error(s) as well as sample input (to trigger the error) and the expected output for that sample input (when there's no error) – refer to [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Take care about your question title; make it as descriptive as possible (refer to [How to Ask a Good Question](https://stackoverflow.com/help/how-to-ask)). – Ivo Mori Jul 13 '20 at 01:44
  • Also take care with choosing the question tags. Only include the ones which are really necessary and relevant. Your problem is a bash scripting problem; it's unrelated to `ubuntu` or to your editor `vim`. // I also suggest you have a look at [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Ivo Mori Jul 13 '20 at 01:47

2 Answers2

1

This line should not have spaces between the = sign:

grades = $(($assignment + $testo + $testa + $finalexam))

Change that line to:

grades=$(($assignment + $testo + $testa + $finalexam))
LeadingEdger
  • 604
  • 4
  • 7
0

A sample execution of your script returns following (error) output

sh ./test.sh 
Enter assignment mark (0 to 40):20
Enter Test1 mark (0 to 15):3
Enter Test2 mark (1 to 15):10
Enter Final exam mark (1 to 30):20
./test.sh: line 8: grades: command not found
./test.sh: line 9: [: -ge: unary operator expected
./test.sh: line 12: [: -ge: unary operator expected
./test.sh: line 15: [: -ge: unary operator expected
./test.sh: line 18: [: -ge: unary operator expected
./test.sh: line 21: [: -ge: unary operator expected
./test.sh: line 24: [: -ge: unary operator expected
./test.sh: line 27: [: -ge: unary operator expected
./test.sh: line 30: [: -ge: unary operator expected
./test.sh: line 33: [: -ge: unary operator expected
./test.sh: line 36: [: -ge: unary operator expected
./test.sh: line 39: [: -ge: unary operator expected
./test.sh: line 42: [: -ge: unary operator expected
./test.sh: line 45: [: -ge: unary operator expected
Wrong input

Where the error ./test.sh: line 8: grades: command not found implies that there must be something wrong with your grades assignment (and possibly the calculation).

grades = $(($assignment + $testo + $testa + $finalexam))

If you want to use this ((result=a+b)) bash syntax for your integer arithmetic and variable assignment then you need to change it to

((grades = assignment + testo + testa + finalexam))

Then the sample output will be as expected

sh ./test.sh 
Enter assignment mark (0 to 40):20
Enter Test1 mark (0 to 15):3
Enter Test2 mark (1 to 15):10
Enter Final exam mark (1 to 30):20
Your final grade is D

Addendum

As pointed out @BenjaminW. in the comment Another thing to fix (and maybe point out) is the quoting of variables in [...]; in your if-statement(s)

if [ $grades -ge 0 ] && [ $grades -lt 49 ]

you reference to your $grades variable without enclosing it in double quotes. That's usually not a good idea (and a source of errors as well as confusion) because referring to a variable without double quotes allows for reinterpretation (of therein contained special characters) and word splitting (if there are space characters) rather than simply only replacing the variable with its value – which is what you actually want. Thus, your if-statement(s) should use "$grades" instead as in

if [ "$grades" -ge 0 ] && [ "$grades" -lt 49 ]

But there's more. Because you're using Bash prefer using [[ ]] instead of [ ]; and then you also don't need to expand your variable with $ for your arithmetic comparisons. With this your if-statement becomes

if [[ grades -ge 0 && grades -lt 49 ]] 

Now given that you want to do arithmetic comparisons you should actually use (( )), and then your if-statement looks like this

if (( grades >= 0 && grades < 49 ))

The revised Bash script is then this

#!/bin/bash

read -p "Enter assignment mark (0 to 40):" assignment
read -p "Enter Test1 mark (0 to 15):" testo
read -p "Enter Test2 mark (1 to 15):" testa
read -p "Enter Final exam mark (1 to 30):" finalexam

((grades = assignment + testo + testa + finalexam))

if (( grades >= 0 && grades < 49 )) 
then
        echo "Your final grade is F"
elif (( grades >= 50 && grades < 52 ))
then
        echo "Your final grade is D-"
elif (( grades >= 53 && grades < 56 ))
then 
        echo "Your final grade is D"
elif (( grades >= 57 && grades < 59 ))
then 
        echo "Your final grade is D+"
elif (( grades >= 60 && grades < 62 ))
then 
        echo "Your final grade is C-"
elif (( grades >= 63 && grades < 66 ))
then 
        echo "Your final grade is C"
elif (( grades >= 67 && grades < 69 ))
then 
        echo "Your final grade is C+"
elif (( grades >= 70 && grades < 72 ))
then 
        echo "Your final grade is B-"
elif (( grades >= 73 && grades < 76 ))
then 
        echo "Your final grade is B"
elif (( grades >= 77 && grades < 79 ))
then 
        echo "Your final grade is B+"
elif (( grades >= 80 && grades < 84 ))
then 
        echo "Your final grade is A-"
elif (( grades >= 85 && grades < 89 ))
then 
        echo "Your final grade is A"
elif (( grades >= 90 && grades < 100 ))
then 
        echo "Your final grade is A+"
else
echo "Wrong input"
fi

These are further helpful existing Q/As to refer to:

Ivo Mori
  • 2,177
  • 5
  • 24
  • 35
  • _Thank you so much, it finally worked !_ – Happy to help. But there's no need to say thanks in the comments (anymore). You can tick the _Say thanks for this answer_ icon next to the answer instead (see also [blog post on it](https://stackoverflow.blog/2020/06/17/saying-thanks-testing-a-new-reactions-feature/). Otherwise, you can simply up-vote an answer to "say thanks" ;) – Ivo Mori Jul 13 '20 at 02:10
  • Another thing to fix (and maybe point out) is the quoting of variables in `[...]`. – Benjamin W. Jul 13 '20 at 02:44
  • @BenjaminW. extended my answer to (hopefully) cover for what you pointed out regarding "quoting of variables". – Ivo Mori Jul 13 '20 at 06:39