LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Newbie bash scripting question (https://www.linuxquestions.org/questions/linux-newbie-8/newbie-bash-scripting-question-4175493215/)

xenodamus 01-30-2014 11:39 PM

Newbie bash scripting question
 
1 Attachment(s)
I'm teaching myself bash scripting from the ground up and got stuck on something. Sorry the code is in an image file. I'm running CentOS inside a VM and couldn't find any way to copy/paste from the terminal with no mouse. (Any suggestions welcome there as well!)

So...the code is supposed to ask you for a number (0-100), evaluate the number as a letter grade (A-F), and finish by asking if you'd like repeat.

Everything executes fine the first go round, but when the while loop repeats, none of the if statements are executed. It jumps straight to the end and asks if you'd like to evaluate another number.

What am I missing there?

pan64 01-31-2014 01:14 AM

read GRADE is outside of the loop. I think this is the problem.

grail 01-31-2014 01:42 AM

You only read the new value for GRADE at the start :)

markush 01-31-2014 01:44 AM

As pan64 says, put all "read" statements only into the loop.

Furthermore you have too many tests within the elif statements. If a number is already tested to be greater then 50 within the while loop, you'll only have to check if it is less than 60, because it cannot be less than 50 at this line of the while loop. It should be
Code:

if [GRADE -lt 50 ]
...
elif [ GRADE -lt 60 ]
...
elif [ GRADE -lt 70 ]
...

Markus

xenodamus 01-31-2014 11:54 AM

Wow...shame on me for not seeing that. I don't suppose a loop would execute statements unless they're contained within... :)

@markush - Thanks for the suggestion...good eye. Hopefully my efficiency and elegance improve as I continue to practice.


All times are GMT -5. The time now is 12:56 PM.