On 12/26/2007 5:05 PM, Grant wrote:
> On Wed, 26 Dec 2007 21:36:48 +0000 (UTC), gazelle@[EMAIL PROTECTED]
(Kenny McCormack) wrote:
>
>
>>In article <ppf5n3pkc4v6elkicjp8arsrff7rp6vrtg@[EMAIL PROTECTED]
>,
>>Grant <bugsplatter@[EMAIL PROTECTED]
> wrote:
>>...
>>
>>>Move the first tick:
>>># awk /$VAR/ '{ print $0 }' myfile
>>
>>Others will chime in and say that this is a bad idea (security, etc,
etc).
>>That is VAR has some weird value (with "rm -rf" and such in it), it will
>>cause something horrible to happen, etc, etc.
>>
>>But the fact is, it is perfectly safe for normal user code; you can
>>usually control it OK, and, more to the point, the alternative, using
>>"-v" is just as vulnerable to the same hack(s).
>>
>>The only truly safe way to do it is to pass it in the environment.
>>Well, that's safe in an AWK sense, but of course, you then have to worry
>>about the syntax of your "ex****t foo=..." statement...
>>
>>But the real answer is that you almost certainly don't need to do this
>>at all, if you are using AWK correctly. I've found that most of the
>>time, when this question is asked, the answer is "Learn and use AWK
>>correctly, and you won't need to do it".
>
>
> Okay, I have this running for some weeks as a cron job:
>
> # pull rx and tx bytes from /proc/net/dev:
> # rx tx
> # ppp0:22921966 17725 0 0 0 0 0 0 444056 10437 0 0 0 0 0 0
> #
> # output line: date rx_bytes tx_bytes, or date 0 0 when interface down
> #
> CONF="/etc/netdraw.conf"
> INTERFACE=$(awk '$1=="interface"{print $2}' $CONF)
> SAMPLEFILE=$(awk '$1=="samplefile"{print $2}' $CONF)
> #
> printf "%s " $(date +%F.%H:%M) >> $SAMPLEFILE
>
> awk /$INTERFACE:/'{f++;sub(/:/," ");print $2,$10};END{if(!f)print 0,0}'
\
> /proc/net/dev >> $SAMPLEFILE
>
> Bash, if it matters, yes I can do the whole thing in awk but it looks
even
> uglier?
It's hard to say for sure without knowing what's in /etc/netdraw.conf and
some
more sample input+output, but it looks like this will do what you want:
awk 'NR==FNR {
if ($1 == "interface") interface=$2
if ($1 == "samplefile") samplefile=$2
next
}
$0 ~ interface":" { rx=$2; tx=$10 }
END { print strftime("%F.%H:%M"),rx+0,tx+0 > samplefile }'
/etc/netdraw.conf
/proc/net/dev
which doesn't seem particularly ugly to me, but in any case Kenny's point
wasn't
that you should abandon everything else and only write awk, just that if
you're
going to use awk it's worth taking the time to learn to use it correctly.
Ed.


|