You are not logged in.
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
What is coproc? Also post some sample output.
1000
Offline
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
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
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 .
How could I simulate EOF to force awk to print the line in the file every time?
Offline
I found it:
ping 201.24.94.254 | awk -v nf="net_stat" '{print $NF > nf} {close(nf)}'
thanks for the help.
Offline