[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [MiNT] counting up in bash



Hi!

On Sun, Dec 08, 2002 at 03:03:48AM +0100, Bohdan Milar wrote:
> I write some bash scripts and need to do some calculations. For
> example I have a file containing the 3 following lines:
> 
> 15
> 25
> 20
> 
> I need to have the number 60, as their sum, in some variable.
> How should I do that? Could somebody tell me which tool and how
> to use?

If you really don't care whether it's bash-only, I'd suggest the
following

sum=0
while read -r number
do
	sum=$(($sum + $number))
done < file-with-numbers
echo "Sum is: $sum"


Ciao

Thomas


-- 
Thomas Binder (Gryf @ IRCNet)
gryf@hrzpub.tu-darmstadt.de
PGP-key available on request!