Grades exercise

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

Write a shell script program that will input a name and a mark between 0 and 100. The program then displays the name, the mark, and a letter grade for the mark, as per the following grade cut formula.

  1. Relevant commands, code, scripts, algorithms:

case/read/echo/if

  1. The attempts at a solution (include all code and scripts):

I have a program that i need to get done that gets the person's name and his grade then prints it in this order "name-grade-gradeletter" so i wrote this code:

#!/bin/bash
while :; do
read -p "Enter the person's name: " name
read -p "Enter the grade of the person: " grade

case $grade in
[50-59]) echo "$name $grade F";;
[60-69]) echo "$name $grade C";;
[70-79]) echo "$name $grade B";;
[80-100]) echo "$name $grade A";;
*) echo "Wrong input"

esac
done
echo "Goodbye"

So when you input the name and the grade, it just skips to the asterisk and outputs "Wrong input". Shouldn't it go through the values since the case is $grade and is using grade that is given by the person ?

Can someone tell me my mistake ?

P.S i tried changing the case to if command to see if it works but it shows me that no such file exists ?:

if [$grade >= 50||$grade < 60]; then
echo "$name $grade F"
elif [$grade >= 60||$grade < 70]; then
echo "$name $grade C"
elif [$grade >= 70||$grade < 80]; then
echo "$name $grade B"
elif [$grade >= 80||$grade <= 100]; then
echo "$name $grade A"
else
echo "Wrong input"
fi
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Sheridan college, Oakville, canada, Syed tanbeer, ["https://academics.sheridancollege.ca/programs/computer-systems-technology-software-development-and-network-engineering"

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

The bracket, [ and ] characters, must have at least one space (or end of line) surrounding them - [ is a keyword just like if

1 Like

Your logic is correct, you syntax/punctuation is faulty.
Using the case statement:
Somehow I think that the 50-59 mark should be "D" and less than 50 an "F".
The error is in this part "[50-59]". Do the comparison one character at a time rather than a numeric test.
Using if statements:
Spaces in the test statement are important.
What operators are used for numeric tests, and which for alphabetic?

@jgt The grades are weird but thats how the exercise is given, haha.

Thank you guys!

P.S So after i added the spaces i am still getting this error "grades.sh: line 6: [: missing `]'"

if [ $grade > = 50 || $grade < 60 ]; then
echo "$name $grade F"
elif [ $grade > = 60 || $grade < 70 ]; then
echo "$name $grade C"
elif [ $grade > = 70 || $grade < 80 ]; then
echo "$name $grade B"
elif [ $grade > = 80 || $grade <= 100 ]; then
echo "$name $grade A"
else
echo "Wrong input"
fi

Hi,

You are using a loop that is never exited, no matter what the input.
Also, there is a difference between a string comparison and a numerical comparison.
A case statement can only match patterns. Like @jgt mentioned, in that syntax a pattern like [50-59] will match a single character, in this case when it is 5, 0-5 or 9.

The [ ... ] syntax does not allows for the use of || within the brackets.

You would need to use

if [ condition1 ] || [ condition2 ]; then

For numerical comparisons keywords like -eq , -lt , -le and such are used (see man test )

--
Note: Be careful with spaces < = is not the same as <=

1 Like

now its doing this, lets say name is abc, grade is 76

it would print abc 76 F not abc 76 C

if [ $grade -ge 50 ] || [ $grade -lt 60 ]; then
echo "$name $grade F"
elif [ $grade -ge 60 ] || [ $grade -lt 70 ]; then
echo "$name $grade C"
elif [ $grade -ge 70 ] || [ $grade -lt 80 ]; then
echo "$name $grade B"
elif [ $grade -ge 80 ] || [ $grade -le 100 ]; then
echo "$name $grade A"
else
echo "Wrong input"
fi

Consider the difference between || and &&

1 Like

OMG! i should have known.. thank you!

Any idea on how to proceed with the (more elegant?) case method?

The problem in the case options you have defined is probably that it will be matching a regular (textual) expression, not a numeric range. Consider the characters that your variable is range than the value.

Does that help? A good case statement can make a wonderful replacement to if...then...elif...elif...elif...fi or lots of separate and confusing if statements. Just as good programming technique, your should follow this though and I would expect you would to get a better mark if you can crack it.

Robin