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 :))

Recommended Answers

All 2 Replies

You're already running AWK, make it do the additions

top | grep root | awk '{ sum += $10 }
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.

better still, no need grep

top | awk '/root/{ sum += $10 }END {print sum}'
commented: Indeed :) +20
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.