LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-19-2012, 01:22 PM   #1
demet8
LQ Newbie
 
Registered: Sep 2012
Posts: 9

Rep: Reputation: Disabled
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]}"

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


echo $total / 5
done
 
Old 10-19-2012, 01:39 PM   #2
rtmistler
Senior Member
 
Registered: Mar 2011
Location: Milford, MA. USA
Distribution: MontaVista, Ubuntu, MINT
Posts: 1,026
Blog Entries: 7

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
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:

#!/bin/bash

set -x
set -v

total=449
echo `expr $total / 5`

Output:
+ set -v

total=449
+ total=449
echo `expr $total / 5`
expr $total / 5
++ expr 449 / 5
+ echo 89
89
 
Old 10-19-2012, 03:01 PM   #3
cbtshare
Member
 
Registered: Jul 2009
Posts: 561

Rep: Reputation: 42
There is a much shorter to do it:
store1=100
store2=75
store3=74
store4=100
store5=100

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

echo $equal/3| bc
 
Old 10-19-2012, 03:07 PM   #4
rtmistler
Senior Member
 
Registered: Mar 2011
Location: Milford, MA. USA
Distribution: MontaVista, Ubuntu, MINT
Posts: 1,026
Blog Entries: 7

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Quote:
Originally Posted by cbtshare View Post

echo $equal/3| bc
You mean "/5" there by the way.
 
Old 10-20-2012, 06:36 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
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:
Code:
echo $((store1 + store2 + store3 + store4 + store5 ))
Or if you want to be a little bit cheeky:
Code:
nums=( 100 75 74 100 100 )

IFS='+'

echo $(( ${num[*]} ))
 
Old 10-20-2012, 01:46 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
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.

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

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

echo $total / 5
done
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.

Code:
"${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.

Code:
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.

Code:
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:

http://mywiki.wooledge.org/ArithmeticExpression
http://wiki.bash-hackers.org/syntax/arith_expr

Last edited by David the H.; 10-20-2012 at 01:55 PM. Reason: minor formatting fixes
 
1 members found this post helpful.
  


Reply

Tags
bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script error : integer expression expected linuxandtsm Linux - Newbie 7 01-07-2014 06:01 AM
Bash script integer value problem [SOLVED] luboiron Programming 3 09-14-2012 05:41 AM
[SOLVED] Script to sum values across columns if they have the same row title kmkocot Programming 7 08-05-2012 07:19 PM
converting string in integer in bash script dziadgba Linux - Newbie 5 08-31-2009 05:59 PM


All times are GMT -5. The time now is 09:00 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration