#/bin/bash
####################################################################### 
# Name: test_average.sh
#
# Requirements: None
#
# Description: Simple script to illustrate "while" loop
# 
# Usage: test_average.sh
#
# Notes: Modified for use with Bash presentation for local Lug group.
#
# Author: Unknown, found at
# http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html
#
# License: GPL
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
####################################################################### 

# Calculate the average of a series of numbers.

SCORE="0"
AVERAGE="0"
SUM="0"
NUM="0"

while true; do

	echo -n "Enter your score [0-100%] ('q' for quit): "; read SCORE;

	if (("$SCORE" < "0"))  || (("$SCORE" > "100")); then
		echo "Be serious.  Common, try again: "
	elif [ "$SCORE" == "q" ]; then
		echo "Average score: $AVERAGE%."
		break
	else
		SUM=$[$SUM + $SCORE]
		NUM=$[$NUM + 1]
		AVERAGE=$[$SUM / $NUM]
	fi
done
echo "Exiting."

