|
From: | Matthew Woehlke |
Subject: | Re: Please advise on programming tactics/strategy |
Date: | Tue, 18 Dec 2007 20:57:44 -0600 |
User-agent: | Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.9) Gecko/20071031 Thunderbird/2.0.0.9 Mnenhy/0.7.5.0 |
cga2000 wrote:
I was wondering if there is any way I can convince netstat to return its output to bash variables for processing. Pretty simple logic: Do forever:Call netstat to obtain RX & TX byte counts Print delta {current .. minus previous .. byte counts} Save current byte counts Wait for a second or so ..I initially thought I could just pipe the "netstat -in" command to the invocation of a bash function. The function would have taken care of the gory details of parsing & formatting the output of the netstat command and would then have stored the current byte counts where they would be available for the next time the function is invoked. The trouble is that I haven't been able to find anything like a "static" local variable that is not reinitialized every time the function is invoked.
Pipes run as seperate processes, e.g. in 'somecommand | function', 'function' is run as a subshell, so anything done in 'function' does not propagate up to the parent. You might want to look at bash's command redirection instead.
Alternatively, something like 'foo=$(netstat | extract)' where 'extract' filters the netstat output and condenses it down to something more easily parsed in the parent, e.g. 'TX:RX'. Then you can do something like 'tx=${foo%:*}; rx=${foo#*:}'.
-- Matthew "Who wants to sing?" -- Orcs (Warcraft II)
[Prev in Thread] | Current Thread | [Next in Thread] |