inserting and summing several values into variable

Reply

Join Date: Dec 2007
Posts: 17
Reputation: yair7190 is an unknown quantity at this point 
Solved Threads: 0
yair7190 yair7190 is offline Offline
Newbie Poster

inserting and summing several values into variable

 
0
  #1
Aug 16th, 2008
hi everyone,

im searching for a way to insert several values i get from a command [top |grep root |awk '{print $10}'] to a variable, so that the variable will contain the sum of all values returned.

(trying to calculate MEM usage by all root processes )
can anyone help?


p.s - why when i run that command i need to press q to get the result? and when i type:
ps -ef |grep root |awk '{print $10}'
it returns automatically?

thanx a lot !!
yair )
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: inserting and summing several values into variable

 
0
  #2
Aug 17th, 2008
You're already running AWK, make it do the additions
Shell Scripting Syntax (Toggle Plain Text)
  1. top | grep root | awk '{ sum += $10 }
  2. END {print sum}'
Yes, you need that newline in the command

> p.s - why when i run that command i need to press q to get the result?
Because 'top' is an interactive program?
You'd have to read the manual to see if there's a "do it once and quit" option.
But I'd use 'ps' if it works.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: inserting and summing several values into variable

 
1
  #3
Aug 19th, 2008
better still, no need grep
Shell Scripting Syntax (Toggle Plain Text)
  1. top | awk '/root/{ sum += $10 }END {print sum}'
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC