Link to home
Start Free TrialLog in
Avatar of sharonp
sharonp

asked on

Output awk to route delete

I need to write a shell script similar to the following:
netstat -rn | awk '{if ($3 = "UGHD") print | route delete $1, $2}'

I can't seem to get the command route delete to accept the output from awk correctly.  What's wrong with my syntax?
Avatar of leandro
leandro

Try the folowing simple script:
#!/bin/ksh

netstat -m | awk '{if ($3 == "UGHD") print $1,":",$2 > "output.txt"}'
FIRST=`cut -d: -f1 output.txt`
SECOND=`cut -d: -f2 output.txt`

route delete $FIRST $SECOND

Good luck!
Leandro
Avatar of sharonp

ASKER

Leandro - I forgot to add the stipulation that we do not want to create an intermediate file in the process.  Why won't this command work?

netstat -rn | awk '"UGHD" {system (route delete $1 $2)}'
`netstat -rn | awk '{ if ($3 == "UGHD") print "route delete",$1,$2 }'`
Avatar of sharonp

ASKER

Thanks, Seedy - I tried your command, and it didn't work, but it did put us on the right track.  The use of commas following the route delete command causes an error that there are too many parameters.  Instead, we can use ..."route delete "$1" "$2}' - Unix liked that much better.  The actual command we added to our cron job ended up being: netstat -rn | awk '$3 = /UGHD/ { system("route delete "$1" "$2)}'.

Thanks for leading us in the right direction!
Avatar of sharonp

ASKER

Seedy - I need for you to post the answer so I can award the points!
I think that solution proposed by seedy is not what sharonp wants.
The "route delete $1 $2" should be executed and not printed
My new solution is:

  for i in `netstat -m | awk ´{if ($3 == "UGHD") print $1":"$2}´`
  do
  FIRST=`echo $i | cut -d: -f1`
  SECOND=`echo $i | cut -d: -f2`
  route delete $FIRST $SECOND
  done

  This can be done on command line or script
  It´s simple but i think that works.

  Regards.
  Leandro.
ASKER CERTIFIED SOLUTION
Avatar of seedy
seedy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial