-   Linux - Newbie (
-   -   bash script to add integer values & get the sum (

demet8 10-19-2012 01:22 PM

bash script to add integer values & get the sum
I need to write a bash script that adds the current values and returns the average of the values. 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:

for var in "${sum[store1=100, store2= 75, store3= 74, store4= 100, store5= 100]}"

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

echo $total / 5

rtmistler 10-19-2012 01:39 PM

First insert echo $total in there to validate that you're computing total correctly.
One thing I also do in any script is I add:

#set -v
#set -x

Lines to every script and uncomment those lines to see debug flow of my script when I have problems.

One correct way to output that result is:

echo `expr $total/5`

Check the Arithmetic Expansion part of the Bash Scripting guide.

I know that the following script does work:


set -x
set -v

echo `expr $total / 5`

+ set -v

+ total=449
echo `expr $total / 5`
expr $total / 5
++ expr 449 / 5
+ echo 89

cbtshare 10-19-2012 03:01 PM

There is a much shorter to do it:

equal=$(echo "$store1+ $store2 + $store3 + $store4 + $store5" | bc)

echo $equal/3| bc

rtmistler 10-19-2012 03:07 PM


Originally Posted by cbtshare (Post 4810200)

echo $equal/3| bc

You mean "/5" there by the way.

grail 10-20-2012 06:36 AM

Well strictly speaking cbtshare has shown the additionally requested detail, ie. how to get the average, but the answer to the simplest
way to get the sum of integers, ie the original question, would be:

echo $((store1 + store2 + store3 + store4 + store5 ))
Or if you want to be a little bit cheeky:

nums=( 100 75 74 100 100 )


echo $(( ${num[*]} ))

David the H. 10-20-2012 01:46 PM

Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

That goes for everyone in this thread.


for var in "${sum[store1=100, store2= 75, store3= 74, store4= 100, store5= 100]}"

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

echo $total / 5

Where did you learn this code? I'm sorry to say, but other than the basic for loop syntax, it's all wrong. It almost looks to me like you're conflating shell syntax with that of some other programming language.


"${sum[store1=100, store2= 75, store3= 74, store4= 100, store5= 100]}"
This is actually an array variable expansion ( "${arrayname[index]}" ). The [] index brackets in this pattern do act in an arithmetic context, but all that the arithmetic expression above does is set five variables. The index expression as a whole expands nothing, so the array variable expansion also ends up as a null value. The for loop has nothing to work on.

But what's the point of the loop anyway? Even if it worked, it would only operate on a single value at a time (with that value being stored in the "$var" variable), but your subsequent lines indicate that you want to add all five values at once.


total= $sum(store1+store2+ store3+store4+store5)
Unlike the first line, there is no arithmetic context at work here. The variable total is set to the literal string. Or at least it would if the syntax were correct. "var=value" cannot have any unprotected spaces in it, outside of an arithmetic context.


echo $total / 5

Again, there's no arithmetic context involved, even if the "$total" variable did contain a valid integer, which it doesn't. echo would only print the literal string (more specifically, $total would be expanded first, then each word in the argument list would be printed as a separate string, with spaces between them).

As pointed out before, proper arithmetic operations are usually done inside $((..)) brackets, although there are other variations.

For full details on shell arithmetic, see these links:

All times are GMT -5. The time now is 07:29 PM.