Grades exercise

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Grades exercise
# 1  
Old 04-14-2018
Question 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.

2. Relevant commands, code, scripts, algorithms:

case/read/echo/if

3. 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:

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 ?:

Code:
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



4. 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).
# 2  
Old 04-14-2018
The bracket, [ and ] characters, must have at least one space (or end of line) surrounding them - [ is a keyword just like if
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 04-14-2018
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?
# 4  
Old 04-15-2018
@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 `]'"

Code:
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


Last edited by Scrutinizer; 04-15-2018 at 11:17 PM.. Reason: Code tags
# 5  
Old 04-15-2018
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
Code:
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 <=

Last edited by Scrutinizer; 04-16-2018 at 12:03 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 04-16-2018
now its doing this, lets say name is abc, grade is 76

it would print abc 76 F not abc 76 C

Code:
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


Last edited by Scrutinizer; 04-16-2018 at 12:10 AM.. Reason: code tags
# 7  
Old 04-16-2018
Consider the difference between || and &&
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

UNIX Exercise

Hi, I am learning unix. I want to practice few small excercises. Please suggest me some goor URL to practice. Thanks in advance. (1 Reply)
Discussion started by: stew
1 Replies

2. Shell Programming and Scripting

A very tough exercise

hello everyone!:) I have an exercise which I think is difficult for beginner like me. Here is the exercise Create a shell script, which takes a directory as command line argument. Script displays ten first lines from every text file in that directory. After displaying the lines from the... (1 Reply)
Discussion started by: googlevn
1 Replies

3. Homework & Coursework Questions

Help with this exercise

you are to write a program which will read in a tax rate (as a percentage) and the prices of 5 items. the program is to calculate the total price, before tax, of the items and then the tax payable on those items, and then the total amount due. the tax payable is computed by appliying the tax rate... (1 Reply)
Discussion started by: bunkercrazy
1 Replies

4. Shell Programming and Scripting

Trouble with part of an exercise

Hi, 'm trying to do an exercicise, and one part is: ls -l $1 | awk ' BEGIN { max = $5; } { if ($5 > max){ max = $5; } } END { print "Tamanio mayor fichero = " max; }' # Imprimimos ahora el menor tamaņo de fichero ls -l $1 | awk '... (4 Replies)
Discussion started by: Phass
4 Replies

5. Shell Programming and Scripting

Reading 5 Students names and grades using while loop????

How do I write a shell script file to read 5 student names (First and Last name) and their grades using a While Loop? Find the Average, Maximum, and Minimum of grades using the same While Loop. Prompt the user using tput to enter the information (first name, last name, grade). Save the data in a... (1 Reply)
Discussion started by: dlbomber1
1 Replies

6. UNIX for Dummies Questions & Answers

[question] hard exercise, help needed

Hello guys. Well, on this exercise i need the average "chargeAmount" per hour (for each hour). with this code : cat getusagesummarywrongmatch | grep -iv MOU2GRTObject | cut -d'|' -f4,14 | grep -i chargeamount | cut -d' ' -f2 http://img227.imageshack.us/img227/5889/65969235do0.jpg i got... (2 Replies)
Discussion started by: EnioMarques
2 Replies

7. UNIX for Dummies Questions & Answers

[question] trouble with an 'exercise'

Hello guys.. well, im kinda newbie with unix because i started to learn it like 2 weeks ago. then i started to make some exercises, but i got stucked on this one : so, i need to know how many different 'names' has the 5th field and how many times each name appears. i was trying with a... (6 Replies)
Discussion started by: EnioMarques
6 Replies
Login or Register to Ask a Question