[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 161
  • 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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