From: Seebs on 4 Nov 2009 08:59 On 2009-11-04, Kaz Kylheku <kkylheku(a)gmail.com> wrote: > I don't see any obvious bash dependency. The nestable command substitution > syntax $(...) is POSIX. Yes, although not as portable as one might wish. (Although I hear rumors the next SunOS finally abandons the pre-POSIX /bin/sh.) -s -- Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: Maxwell Lol on 4 Nov 2009 11:53 >> #!/bin/bash >> >> /usr/bin/netstat -an | grep TIME_WAIT > time.wait.file >> >> while read line >> do >> LOCAL=`echo $line | awk '{print $1}'` >> LOCALIP=`echo $LOCAL | cut -d "." -f 1-4` >> LOCALPORT=`echo $LOCAL | cut -d "." -f 5` >> REMOTE=`echo $line | awk '{print $2}'` >> REMOTEIP=`echo $REMOTE | cut -d "." -f 1-4` >> REMOTEPORT=`echo $REMOTE | cut -d "." -f 5` >> /usr/local/bin/tcpdrop $LOCALIP $LOCALPORT $REMOTEIP $REMOTEPORT >> done < time.wait.file You do realize that you are executing 12 different programs per line. My single-user machine has 300 lines to process. That's 3000+ processes. A serious server will have many more. Why not just have one involcation of awk, instead of thousands? Or use a pure BASH version?
From: Geoff Clare on 5 Nov 2009 08:19 bb wrote: >> awk: syntax error near line 1 >> awk: bailing out near line 1 > On Solaris you should use nawk instead of awk. No. You should just set your PATH appropriately so that the POSIX conforming versions of utilities are found before the historical versions. Then there is no need to use different commands on different systems. PATH=$(getconf PATH):other:stuff -- Geoff Clare <netnews(a)gclare.org.uk>
From: Michael Paoli on 8 Nov 2009 13:55
On Nov 3, 8:33 am, Ciccio <lserena(a)gmail.com> wrote: > Given the output below, > > $ netstat -an | grep TIME_WAIT > 10.159.244.250.80 10.159.244.250.49198 49152 0 49152 0 TIME_WAIT .... > 10.159.244.250.49195 10.159.244.250.1984 49553 0 49152 0 TIME_WAIT > > I need to obtain these 4 variables ($LOCALIP $LOCALPORT $REMOTEIP > $REMOTEPORT) to pass to tcpdrop. > > Here is what I came up with - and it works, but is there a better/ > faster/neater way of doing it? > > #!/bin/bash > > /usr/bin/netstat -an | grep TIME_WAIT > time.wait.file > > while read line > do > LOCAL=`echo $line | awk '{print $1}'` > LOCALIP=`echo $LOCAL | cut -d "." -f 1-4` > LOCALPORT=`echo $LOCAL | cut -d "." -f 5` > REMOTE=`echo $line | awk '{print $2}'` > REMOTEIP=`echo $REMOTE | cut -d "." -f 1-4` > REMOTEPORT=`echo $REMOTE | cut -d "." -f 5` > /usr/local/bin/tcpdrop $LOCALIP $LOCALPORT $REMOTEIP $REMOTEPORT > done < time.wait.file #!/bin/sh /usr/bin/netstat -an | fgrep TIME_WAIT | # space, tab, ., and newline for IFS while IFS=' . ' read -r l1 l2 l3 l4 LOCALPORT r1 r2 r3 r4 REMOTEPORT x do LOCALIP=$l1.$l2.$l3.$l4 REMOTEIP=$r1.$r2.$r3.$r4 /usr/local/bin/tcpdrop $LOCALIP $LOCALPORT $REMOTEIP $REMOTEPORT done Or if one doesn't really need those LOCALIP and REMOTEIP variables set: #!/bin/sh /usr/bin/netstat -an | fgrep TIME_WAIT | # space, tab, ., and newline for IFS while IFS=' . ' read -r l1 l2 l3 l4 LOCALPORT r1 r2 r3 r4 REMOTEPORT x do /usr/local/bin/tcpdrop $l1.$l2.$l3.$l4 $LOCALPORT $r1.$r2.$r3.$r4 $REMOTEPORT done Why use a bunch of stuff that's external to the shell if it's not needed? Do use caution when altering IFS. In this case we modified IFS only and specifically for our read command, which should be relatively safe given what we want to do in this particular case. |