Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
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.
Relevant commands, code, scripts, algorithms:
case/read/echo/if
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
Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
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?
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 <=
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.