• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

adding

hi!
i want to write a shell command that will find the sum of the numbers in a file..
how can i do this?
thanks..
0
btocakci
Asked:
btocakci
  • 3
  • 2
  • 2
2 Solutions
 
sunnycoderCommented:
Hi btocakci,

cat filename | while read number
do
     sum=` expr $sum + $number `
     echo input was $number and current sum is $sum
done

Cheers!
Sunnycoder
0
 
btocakciAuthor Commented:
hi sunnycoder!

an error occurred..
at line three expr+1 : command not found

what is the reason??

thanks
0
 
sunnycoderCommented:
there needs to be a space on the either side of + sign
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
btocakciAuthor Commented:
thanx..
0
 
xDamoxCommented:
Hi,

What does the file content look like?
0
 
btocakciAuthor Commented:
1 3 5 8 9 6 45 65  lets say..
0
 
xDamoxCommented:
Hi,

This works fine on this type of format:

#!/bin/bash
#

for i in `cat test`;
do
        let "Total += $i";
done

echo "The Total is: $Total";
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now