Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash script to calculate average of several integer values

Tags:

bash

shell

I need to write a bash script that adds the current values and returns the average. When I run the script ./average I get the error message: missing}. I am not sure why the script isn't printing the average to screen when I run it.

Here's what I have written thus far:

#! /bin/csh
for var in "${sum[store1=100, store2= 75, store3= 74, store4= 100, store5= 100])}"

do
total= $sum(store1+store2+ store3+store4+store5))


echo $sum / 5
done
like image 429
demet8 Avatar asked Jun 27 '26 19:06

demet8


1 Answers

Fixed: (Updated)

#/bin/bash

total=0
list=(100 75 74 100 100)
for var in "${list[@]}"
do
    total=$((total + var))
done

average=$((total/5))
echo $average
like image 67
sampson-chen Avatar answered Jul 02 '26 06:07

sampson-chen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!