I want to loaded a temporary files with the bunch of numbers from input file. The format of the script is "testing -row/-col [input file]". The input file mostly just a bunch of random numbers like
1 2 3 4
3 3 5 6
9 4 4 2
My code below is trying to grab this input file as argument and then "cat" these numbers into a new temporary files. From there, I'm trying to find average value of the row from this temporary files.
FILENAME=$2
TMP=./TMP2.$$
cat $FILENAME > $TMP
#average row
function avg_row {
while read -a row
do
total=0
sum=0
for i in "${rows[@]}"
do
eum=`expr $sum + $i`
total=`expr $total + 1`
done
average=`expr $sum / $total`
echo $average
done < $TMP
}
However, even though when I "cat" the TMP files it display exactly the same like testing_file, when I run the script it prints
expr: division by zero
expr: division by zero
expr: division by zero
Any suggestions or idea on why this could happen? Thank you.