LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-24-2012, 08:18 PM   #1
marsblackmon
LQ Newbie
 
Registered: Jul 2012
Posts: 1

Rep: Reputation: Disabled
help with a shell script


i am new to linux. i am writing a bash script that get 10 grades, calculates the avg of the 10 grades, shows the highest and lowest grade, as well as turn the avg into a letter grade and finally show the new avg when i eliminate the lowest grade. this is what i have thus far:

Name of script: Grade_Assignment
#=====================================================
#read ten numbers and assigned them to 10 variable


read -p "Enter test score one : " n1
read -p "Enter test score two : " n2
read -p "Enter test score three : " n3
read -p "Enter test score four : " n4
read -p "Enter test score five : " n5
read -p "Enter test score six : " n6
read -p "Enter test score seven : " n7
read -p "Enter test score eight : " n8
read -p "Enter test score nine : " n9
read -p "Enter test score ten : " n10

#=====================================================
((test=$n1+$n2+$n3+$n4+$n5+$n6+$n7+$n8+$n9+$n10))

((avg=test/10))
#=====================================================
# CONVERT AVERAGE TO LETTER GRADE
#=====================================================
if ((avg>=90))
then
grade="A"
echo "Outstanding job."
elif ((avg>=80))
then
grade="B"
echo "Good job"
elif ((avg>=70))
then
grade="C"
echo "You passed, but keep practicing"
elif ((avg>=65))
then
grade="D"
else
grade="F"
fi
#========================================================
echo "Your overall average is:" $avg
echo "Your overall grade is an:" $grade
echo "That's all folks, see you next script."
 
Old 07-24-2012, 09:12 PM   #2
rosehosting.com
Member
 
Registered: Jun 2012
Location: Missouri, USA
Posts: 236

Rep: Reputation: 64
What exactly is not working as you expected?

Quote:
Originally Posted by marsblackmon View Post
i am new to linux. i am writing a bash script that get 10 grades, calculates the avg of the 10 grades, shows the highest and lowest grade, as well as turn the avg into a letter grade and finally show the new avg when i eliminate the lowest grade. this is what i have thus far:

Name of script: Grade_Assignment
#=====================================================
#read ten numbers and assigned them to 10 variable


read -p "Enter test score one : " n1
read -p "Enter test score two : " n2
read -p "Enter test score three : " n3
read -p "Enter test score four : " n4
read -p "Enter test score five : " n5
read -p "Enter test score six : " n6
read -p "Enter test score seven : " n7
read -p "Enter test score eight : " n8
read -p "Enter test score nine : " n9
read -p "Enter test score ten : " n10

#=====================================================
((test=$n1+$n2+$n3+$n4+$n5+$n6+$n7+$n8+$n9+$n10))

((avg=test/10))
#=====================================================
# CONVERT AVERAGE TO LETTER GRADE
#=====================================================
if ((avg>=90))
then
grade="A"
echo "Outstanding job."
elif ((avg>=80))
then
grade="B"
echo "Good job"
elif ((avg>=70))
then
grade="C"
echo "You passed, but keep practicing"
elif ((avg>=65))
then
grade="D"
else
grade="F"
fi
#========================================================
echo "Your overall average is:" $avg
echo "Your overall grade is an:" $grade
echo "That's all folks, see you next script."
 
Old 07-24-2012, 10:11 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,017

Rep: Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196
I agree, what is your question?

As points of interest:

1. Using a loop with an array would reduce over use of single reads

2. Above loop could also perform total calculation

3. bash supports only integer division so avg will always be a whole number (not sure if this is an issue but should be considered)

4. Better to use case in place of multiple if / elif construct (cleaner)
 
Old 07-25-2012, 12:43 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
What grail said.

In case you have trouble with point #4, just remember that case statements use globbing patterns for their conditions. So try to think of the numbers as patterns rather than numerical values.

You could also try setting up a function for the read action, if you want to go that route.

Incidentally, many scripters feel that it's more readable to place the "do/then" keywords on the same line as the "for/while/until/if" keywords, as it more clearly separates the outside block from the inside block.


Finally, please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, bolding, colors, or other fancy formatting.

The unbroken lines you used are causing my screen to side-scroll. Please edit the original and add code tags around it please.
 
  


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
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
Shell Script running in another shell script and then exit dreamervlk Linux - General 3 09-16-2011 06:40 AM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

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

All times are GMT -5. The time now is 08:22 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
Open Source Consulting | Domain Registration