943,648 Members | Top Members by Rank

Ad:
Aug 16th, 2008
0

inserting and summing several values into variable

Expand Post »
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 )
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yair7190 is offline Offline
17 posts
since Dec 2007
Aug 17th, 2008
0

Re: inserting and summing several values into variable

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 19th, 2008
1

Re: inserting and summing several values into variable

better still, no need grep
Shell Scripting Syntax (Toggle Plain Text)
  1. top | awk '/root/{ sum += $10 }END {print sum}'
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: How to format this date UNDER FreeBSD?
Next Thread in Shell Scripting Forum Timeline: New to Bash Scripting - Service Restart





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC