how to get the sum of all the lines in the file

Hi

I have the following file, how I will calculate the sum of all the entries in the file.

> cat abc
2
3
4

now the sum should be 2+3+4 = 9

with bash script :

#!/bin/bash

sum=0
count=0

for i in `cat logfile`; do sum="$sum+$i"; count=`expr $count + 1`; done

sum=$(echo $sum | bc)
echo "Sum=$sum"

The above assumes that the file you want to process is named "logfile" and you have "bc" utility installed. There's a way in perl, too :

open (DATA, $logFile) or die "Error\n";

        my @rawData=<DATA>;
        my ($sum);
        foreach $n (@rawData) {
                
                $sum += $n;
print "Sum : $sum\n";

assumes that $logFile is declared.
There should be a way with awk, but I can't think of it right now.

awk 'sum=sum+$1 END{print sum}' abc

Try awk

awk '{a+=$0}END{print a}' abc

dear all...thanx for your quick responses..... it helped me a lot to solve the issue....