I am writing a shell script to calculate the averages of numbers from a file but for some reason bash is not adding my numbers together.
I have tried multple ways of formatting the sum='expr $sum + $num' but keep getting errors.
while read line
do
sum=0
count=0
average=0
median=0
#read each line, sort it and put it into tempRows file
echo "$line" >> $tempRow
sort $tempRow
#add each number to the sum for avg calculation
for i in $tempRow
do
num=$i
count=`expr $count + 1`
echo "count: $count"
sum=`expr $sum + $num`
echo "sum: $sum"
done
#perform and print average calc at the end of each line
average=`expr $sum / $count`
echo -e "$average\t"
rm -f $tempRow
done < $tempFile
Currently this code gives me following output before quitting
count: 1
expr:non-numeric argument
sum:
expr:syntax error
I am sorting each line before adding to tempRow for a later median calculation A sample file would contain multiple rows and columns of whole numbers and I would print out each rows average and median