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

Open in new window

ipconfig610Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
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
Gerwin Jansen, EE MVETopic Advisor Commented:
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
ipconfig610Author 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

Open in new window


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

Open in new window

0
woolmilkporcCommented:
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

Open in new window

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

Open in new window

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

Open in new window

                                           
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

Open in new window


wmp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.