You are not logged in.

#1 2008-10-30 01:48:46

hack.augusto
Member
From: Brazil
Registered: 2008-08-28
Posts: 124

[solved]awk code work with files but not with standard output

my internet connection is unstable, then to know when the net is on/off i'm writing a script like that:

coproc ping -i 10 201.24.94.254 | awk '{print $NF}' > net_status
while :; do
   if [[ `cat /sys/class/net/eth0/operstate` == "down" ]] then
     print 'offline'
   elif [[ `cat net_status` == "unreachable" ]] then
     print 'on local net'
   else
     print 'on internet'
   fi
done

my problem is when i run this:

coproc ping -i 10 201.24.94.254 | awk '{print $NF}' > net_status

I get a blank net_status file "coproc ping -i 10 201.24.94.254 | awk '{print $NF > net_status}'" doesn't work also, right now the code is less or more like this:

coproc ping -i 10 201.24.94.254 > net
while :; do
 awk '{print $NF}' net > net_status || sed 1d net
...

but I would like to know why the first "way" doesn't work.

thanks

Last edited by hack.augusto (2008-10-30 21:11:30)

Offline

#2 2008-10-30 16:52:00

byte
Member
From: Düsseldorf (DE)
Registered: 2006-05-01
Posts: 2,046

Re: [solved]awk code work with files but not with standard output

What is coproc? Also post some sample output.


1000

Offline

#3 2008-10-30 20:06:39

hack.augusto
Member
From: Brazil
Registered: 2008-08-28
Posts: 124

Re: [solved]awk code work with files but not with standard output

byte wrote:

What is coproc? Also post some sample output.

coproc establish a coprocess job. I use this because I want the script to run the while loop, and the ping process to continue running.

This is what I expected to be in the file:

$ ping 201.24.94.254 | awk '{print $NF}'                       
data.
ms
ms
ms
ms
...

But when I do this:

$ ping 201.24.94.254 | awk '{print $NF}' > net_stat

net_stat is just a blank file

$ cat net_stat

Offline

#4 2008-10-30 20:11:54

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [solved]awk code work with files but not with standard output

Could it be the buffer is not full or did not reach EOF? Try letting it run to the end. ( Just my guess though, I've never used coproc)

Offline

#5 2008-10-30 20:53:09

hack.augusto
Member
From: Brazil
Registered: 2008-08-28
Posts: 124

Re: [solved]awk code work with files but not with standard output

Procyon wrote:

Could it be the buffer is not full or did not reach EOF? Try letting it run to the end. ( Just my guess though, I've never used coproc)

That is exactly what is going on lol .

How could I simulate EOF to force awk to print the line in the file every time?

Offline

#6 2008-10-30 21:11:04

hack.augusto
Member
From: Brazil
Registered: 2008-08-28
Posts: 124

Re: [solved]awk code work with files but not with standard output

I found it:

ping 201.24.94.254 | awk -v nf="net_stat" '{print $NF > nf} {close(nf)}'

thanks for the help.

Offline

Board footer

Powered by FluxBB