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: