Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-26-2017, 06:29 AM
|
#1
|
LQ Newbie
Registered: Apr 2017
Posts: 1
Rep: 
|
Bash shell script - Grades
Hi,
Sorry for the length of this, but I have a homewaork question I thought I had answered until I tried it and got an error message I cant solve. Any help would be greatly apprecisted (I will post question and my attempt to answer it).
Question: Write a shell script that prompts the user for a grade between 0 and 100. The shell script should calculate the corresponding letter for this grade based on the following criteria:
0–49 ¼ F
50–59 ¼ D
60–69 ¼ C
70–79 ¼ B
80–100 ¼ A
Ensure that the shell script continues to prompt the user for grades until the user chooses to exit the shell script.
My Code:
echo "Please enter your grade"
read GRADE
do
case $GRADE
"0-49")
echo "F"
;;
"50-59")
echo "D"
;;
"60-69")
echo "C"
;;
"70-79")
echo "B"
;;
"80-100")
echo "A"
;;
"q")
break
;;
*)
echo "invalid Option"
;;
esac
done
Error Message:
"line 4:syntax error near unexpected token 'do'"
"line 4: 'do'".
Any assistance would be greatly appreciated as the research I have done hasnt illuminated the error and solution.
Thanks
|
|
|
04-26-2017, 06:37 AM
|
#2
|
LQ Guru
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
|
Hi bigdazza,
You don't actually have a loop in there.
Have a look at this - http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html - and try again.
|
|
|
04-26-2017, 06:38 AM
|
#3
|
LQ Guru
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 6,121
|
Did you read the error message?
What do you think the key "do" does?
Where is that key appropriate?
Did you read the BASH man page?
Have you examined the information about posting code here? There are markup tags that would help.
Many here will be willing to HELP you, but there is a policy about not doing anyone else's homework. Answers will have to be carefully crafted to lead you to find the answer yourself. That is not to frustrate you, that is part of our professional ethics standard and is GOOD for you. (Yeah, I hated hearing that phrase also, but it IS.)
Last edited by wpeckham; 04-26-2017 at 06:40 AM.
|
|
|
04-26-2017, 06:53 AM
|
#4
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,940
|
Hi Bigdazza and welcome to LQ.
Please review the LQ Rules and take note of the specific one saying:
Quote:
Do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and searches) and we'll do our best to help. Keep in mind that your instructor might also be an LQ member.
|
Also suggest you check the Site FAQ for information about how to use [code][/code] tags, or use the advanced edit mode and use the menu option.
Please follow the advice of hydrurga and wpeckham and note that LQ members are not here to do your work for you. You have attempted to do this, please continue.
A further suggestion is to put the line "set -xv" as the second line of your script.
A question I do have is "What should the first line of your script be?" What is shown is incorrect. Please look at some sample scripts to first answer that question.
|
|
|
04-26-2017, 12:15 PM
|
#5
|
LQ Addict
Registered: Dec 2013
Posts: 19,872
|
bigdazza,
we are willling to help you but you need to learn a little basic scripting, and also computer logic in general.
the first 2 lines of your shell script are fine, after that it's just nonsense. sorry if that sounds harsh.
you need to check: - did the user actually enter a number between 0 and 100?
- is the number bigger or smaller than <something>?
your script does not do any of that.
if, e.g., i entered 33, it would NOT echo F, because 33 does not equal '0-49'.
you need to have something like
Code:
if (( GRADE < 50 )); then....
etc.
|
|
|
04-26-2017, 12:41 PM
|
#6
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,603
|
Quote:
Originally Posted by Bigdazza
Hi,
Sorry for the length of this, but I have a homewaork question I thought I had answered until I tried it and got an error message I cant solve. Any help would be greatly apprecisted (I will post question and my attempt to answer it).
Question: Write a shell script that prompts the user for a grade between 0 and 100. The shell script should calculate the corresponding letter for this grade based on the following criteria:
0–49 ¼ F
50–59 ¼ D
60–69 ¼ C
70–79 ¼ B
80–100 ¼ A
Ensure that the shell script continues to prompt the user for grades until the user chooses to exit the shell script.
My Code:
Code:
echo "Please enter your grade"
read GRADE
do
case $GRADE
"0-49")
echo "F"
;;
"50-59")
echo "D"
;;
"60-69")
echo "C"
;;
"70-79")
echo "B"
;;
"80-100")
echo "A"
;;
"q")
break
;;
*)
echo "invalid Option"
;;
esac
done
Error Message: "line 4:syntax error near unexpected token 'do'" "line 4: 'do'".
|
While you did post a verbatim homework question you actually DID show your own effort in doing this yourself...well done. Please, do not be discouraged, and continue to try to solve your own problems.
I would highly recommend you visit the bash scripting tutorials at TLDP:
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
http://tldp.org/LDP/abs/html/
The beginners guide will walk you through some things you need to know, and get you up to speed quickly. You're missing the "while" in the while->do->done looping function:
http://tldp.org/LDP/Bash-Beginners-G...ect_09_02.html
Try something like what can be found here, as there's a script on that page that covers such things, that you SHOULD be able to modify (given your efforts), and make work in short order.
http://tldp.org/LDP/Bash-Beginners-G...ect_09_05.html
...and for integer testing, try using
..for a "less than or equal to". Replace with > for (obviously) GREATER than or equal to.
Last edited by TB0ne; 04-26-2017 at 12:42 PM.
|
|
1 members found this post helpful.
|
04-26-2017, 01:20 PM
|
#7
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
ok my
just a little help.
Code:
#!/bin/bash
#grade checker
while [[ $end != 'q' ]] ;
do
read -p "enter points " points
echo "$points"
read -p "enter q to quit" end
done
and, just in case you're wondering.
it is a little tricky but
http://wiki.bash-hackers.org/syntax/pattern
remember [ ] and | usages. test them in using both together.
Last edited by BW-userx; 04-26-2017 at 02:29 PM.
|
|
|
All times are GMT -5. The time now is 06:39 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|