#!/bin/sh
# Calculate mean (average) of integer data

n=0
sum=0

while read -p "Enter a number (^D to quit): " x
do
  sum=`expr $sum + $x`
  # If this fails, it was non-numeric input
  if [ "$?" -eq "0" ]; then
    n=`expr $n + 1`
    echo "Running Average:"
    echo "scale=2;$sum/$n" | bc
    echo
  fi
done

echo "Overall Average:"
echo "scale=2;$sum/$n" | bc

