LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   help with a shell script (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-a-shell-script-4175418486/)

marsblackmon 07-24-2012 08:18 PM

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."

rosehosting.com 07-24-2012 09:12 PM

What exactly is not working as you expected?

Quote:

Originally Posted by marsblackmon (Post 4737174)
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."


grail 07-24-2012 10:11 PM

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)

David the H. 07-25-2012 12:43 PM

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.


All times are GMT -5. The time now is 01:20 PM.