LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 04-26-2017, 06:29 AM   #1
Bigdazza
LQ Newbie
 
Registered: Apr 2017
Posts: 1

Rep: Reputation: Disabled
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:

049 F

5059 D

6069 C

7079 B

80100 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
 
Old 04-26-2017, 06:37 AM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 19.1 MATE
Posts: 8,016
Blog Entries: 5

Rep: Reputation: 2862Reputation: 2862Reputation: 2862Reputation: 2862Reputation: 2862Reputation: 2862Reputation: 2862Reputation: 2862Reputation: 2862Reputation: 2862Reputation: 2862
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.
 
Old 04-26-2017, 06:38 AM   #3
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,046

Rep: Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332
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.
 
Old 04-26-2017, 06:53 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,061
Blog Entries: 13

Rep: Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504
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.
 
Old 04-26-2017, 12:15 PM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 12,533
Blog Entries: 9

Rep: Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393
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.
 
Old 04-26-2017, 12:41 PM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,970

Rep: Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831
Quote:
Originally Posted by Bigdazza View Post
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
Code:
(("$a" <= "$b"))
..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.
Old 04-26-2017, 01:20 PM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,169

Rep: Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a bash shell script from within another script dedman Linux - Software 7 04-24-2010 08:53 PM
in bash shell how to run shell script during startup rammohan04 Red Hat 2 07-31-2009 02:07 AM
bash shell script CheeSen Programming 4 09-07-2008 10:02 AM
bash shell script naka0naka Linux - Newbie 7 05-28-2004 03:06 PM
bash/shell script mikis Programming 2 10-14-2003 11:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration