# Linux/Bash need to add a bunch of numbers with decimals in a FOR loop

I have a script that pulls 10 numbers from a database.  I need to add the 10 numbers together then divide by 10 to get the average of the 10 numbers.  I have a FOR loop in place to calculate by I am having trouble with my code.

Here is a sample of 10 of the values stored in \$MON10
157351.17
144707.73
149378.68
157250.28
21206.67
142330.23
143506.72
141073.77
139303.22
136483.70

``````MON10="\$(\$MYSQL -u\$LSQLUSER -p\$LSQLPASS \$DB -s -N -e "SELECT orig from daily WHERE day='Monday' ORDER BY date desc LIMIT 10;")"

echo \$MON10
for EACHMON in \$MON10
do

MONTOTAL=`expr \$MONTOTAL + \$EACHMON`;

done

echo "Total :" \$MONTOTAL
``````
###### Who is Participating?

Commented:
The first command sequence (the one using "awk") does return correct values for ksh.

So I assume you're using bash? If so try this:
``````MON10="157351.17 144707.73 149378.68 157250.28 21206.67 142330.23 143506.72 141073.77 139303.22 136483.70"

set \$(echo \$MON10 | awk '{split(\$0,A," ")} END {for(e in A) {sum+=A[e]}; printf "%.2f %.2f\n", sum, sum/10}')

MONTOTAL=\$1
MONAVERAGE=\$2

echo  \$MONTOTAL
echo  \$MONAVERAGE
``````
In the second command (the one using "bc") you misspelled the "MON10" variable name - it must be in uppercase in line 2 as it is in line 1, or it must be in lowercase in line 1 as it is in line 2:
``````MON10="157351.17 144707.73 149378.68 157250.28 21206.67 142330.23 143506.72 141073.77 139303.22 136483.70"
echo \${MON10} | sed 's/ /+/g' | bc
``````
or
``````mon10="157351.17 144707.73 149378.68 157250.28 21206.67 142330.23 143506.72 141073.77 139303.22 136483.70"
echo \${mon10} | sed 's/ /+/g' | bc
``````

By the way, if you want to do the division by 10 using "bc:
``````MON10="157351.17 144707.73 149378.68 157250.28 21206.67 142330.23 143506.72 141073.77 139303.22 136483.70"
MONTOTAL=\$(echo \${MON10} | sed 's/ /+/g' | bc) # that's gerwinjansen's solution
MONAVERAGE=\$(echo "scale=2; (" \$(echo \${MON10} | sed 's/ /+/g') ") / 10" | bc
``````

wmp
0

Commented:
I'm sure most people will suggest using "bc", but my favourite tool is and will remain "awk":

1) If the elements of the variable are separated by linefeeds as in your post:

echo "\$MON10" | awk '{sum+=\$0} END {printf "%.2f %.2f\n", sum, sum/10}' |read MONTOTAL MONAVERAGE

echo \$MONTOTAL
echo \$MONAVERAGE

or

echo \$MON10 | awk '{split(\$0,A," ")} END {for(e in A) {sum+=A[e]}; printf "%.2f %.2f\n", sum, sum/10}' | read MONTOTAL MONAVERAGE

echo  \$MONTOTAL
echo  \$MONAVERAGE

(Note there are no quotes around the variable "\$MON10")

2) If they're separated by spaces you must use the second variant above.
Quotes around "\$MON10" don't matter in this case.

You can have more  decimal places, if you wish. Change "%.2f" to e.g. "%.4f" to get 4 places or to whatever "precision" is desired.

By the way, the problems you're facing when using "expr" are due to the fact that "expr" can only do integer arithmetic and thus doesn't understand the decimal point.
0

if \$mon10 has numbers on one line:

echo \${mon10} | sed 's/ /+/g' | bc

if \$mon10 has a number on each line:

echo \${mon10} | sed 's/\$/+/g' | bc
0

Author Commented:
okay I am not sure what I am doing wrong.  I get no result from this.

``````MON10="157351.17 144707.73 149378.68 157250.28 21206.67 142330.23 143506.72 141073.77 139303.22 136483.70"

echo \$MON10 | awk '{split(\$0,A," ")} END {for(e in A) {sum+=A[e]}; printf "%.2f %.2f\n", sum, sum/10}' | read MONTOTAL MONAVERAGE

echo  \$MONTOTAL
echo  \$MONAVERAGE
``````

OR

``````MON10="157351.17 144707.73 149378.68 157250.28 21206.67 142330.23 143506.72 141073.77 139303.22 136483.70"
echo \${mon10} | sed 's/ /+/g' | bc
``````
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.