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 01-13-2014, 11:30 AM   #1
rivelino90
LQ Newbie
 
Registered: Jan 2014
Posts: 6

Rep: Reputation: Disabled
in linux bash how can i calculate the sum of the figures in a number?


for example number 45 4+5=9
 
Old 01-13-2014, 11:38 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Here's one way:
Code:
echo "45" | awk 'BEGIN{FS=""}{for (i=1;i<=NF;i++)x+=$i}END{print x}'
 
3 members found this post helpful.
Old 01-14-2014, 08:36 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,688

Rep: Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259
For fun:
Code:
sum=0
while read -n 1 V; do
  let sum=sum+V
done <<END
45
END
echo $sum
 
2 members found this post helpful.
Old 01-14-2014, 08:50 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,110

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
^ -1 druuna and jpollard... smells like homework and the op made no attempt.
 
1 members found this post helpful.
Old 01-14-2014, 09:08 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
^ -2 schneidz
- late to the party,
- not constructive.

Also: You, schneidz, should point your remark at the OP and not those that answered.

Most people that are new did not read the LQ rules and they don't have a clue what your remarks is about.

The people that answer should know about the "homework rule", but it is also up to them to decide if it is homework or not, which isn't always clear.

Last edited by druuna; 01-14-2014 at 10:20 AM. Reason: Added some constructiveness.
 
2 members found this post helpful.
Old 01-14-2014, 11:06 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,496

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Well we could tidy the bash up a little:
Code:
while read -n 1 V; do
   (( sum += V ))
done<<<$1

echo $sum
 
1 members found this post helpful.
  


Reply


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
writing a script to calculate sum of all files sumeet inani Linux - Newbie 7 04-09-2013 06:46 PM
[SOLVED] How to calculate the number of *necessary* bits in a byte ? TheIndependentAquarius Programming 16 06-16-2010 01:36 AM
Getting SUM in Bash thewebbie Programming 1 05-20-2009 11:42 AM
I need to calculate the sum of filesizes from a list of files paulBottomley Linux - Newbie 5 04-25-2008 12:19 PM
Calculate total number of mailq items a1phanumeric Linux - Newbie 1 11-15-2007 11:29 AM


All times are GMT -5. The time now is 08:01 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration