LinuxQuestions.org
Help answer threads with 0 replies.
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 01-31-2014, 12:39 AM   #1
xenodamus
LQ Newbie
 
Registered: Dec 2013
Distribution: kubuntu
Posts: 4

Rep: Reputation: Disabled
Newbie bash scripting question


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?
Attached Thumbnails
Click image for larger version

Name:	code.png
Views:	14
Size:	6.9 KB
ID:	14649  
 
Old 01-31-2014, 02:14 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,509

Rep: Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434
read GRADE is outside of the loop. I think this is the problem.
 
1 members found this post helpful.
Old 01-31-2014, 02:42 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
You only read the new value for GRADE at the start
 
1 members found this post helpful.
Old 01-31-2014, 02:44 AM   #4
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
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
 
1 members found this post helpful.
Old 01-31-2014, 12:54 PM   #5
xenodamus
LQ Newbie
 
Registered: Dec 2013
Distribution: kubuntu
Posts: 4

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


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
Newbie Bash Scripting Question jsaxton86 Programming 2 11-26-2005 08:58 PM
Newbie Bash scripting question Caidence Programming 2 08-03-2003 05:53 AM
Newbie bash scripting question... cmfarley19 Linux - Newbie 2 05-03-2003 09:26 AM


All times are GMT -5. The time now is 10:24 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