linux - listening on netcat works but not grep'able (or several other utilities) -
i'm testing netcat udp shell tools , trying take output , send through standard pipe stuff. in example, have netcat client sends 'foo' 'bar' 'foo' newlines each attempt reading listener:
[root@localhost ~ 05:40:20 ]# exec 5< <(nc -d -w 0 -6 -l -k -u 9801) [root@localhost ~ 05:40:25 ]# awk '{print}' <&5 foo bar foo ^c [root@localhost ~ 05:40:48 ]# awk '{print}' <&5 | grep foo ^c [root@localhost ~ 05:41:12 ]# awk '{print}' <&5 | grep --line-buffered foo ^c [root@localhost ~ 05:41:37 ]# [root@localhost ~ 05:44:38 ]# grep foo <&5 foo foo ^c [root@localhost ~ 05:44:57 ]#
i've checked --line-buffered... , same behavior 'od -bc', ie, nothing @ all. grep works on fd... if pipe grep (like od -bc, cut), same (nothing). tried prefixing last grep stdbuf -ol no avail. > file , tail -f it, feel i'm missing something.
update: appears descriptor/order/timing related. created file 'tailsource' , used instead, produced same issue (no output) when ran echo -e "foo\nfoo\nbar\nfoo" >> tailsource
[root@localhost shm 07:16:18 ]# exec 5< <(tail -n 0 -f tailsource) [root@localhost shm 07:16:32 ]# awk '{print}' <&5 | grep foo
... , when run without '| grep foo', output i'd expect. (gnu bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu))
awk
buffering when output not going terminal. if have gnu awk, can use fflush() function flush after every print
gawk '{print; fflush()}' <&5 | grep foo
in particular case though, don't need awk , grep, either do.
awk /foo/ <&5 grep foo <&5
see bashfaq 9 more on buffering , how work around it.
Comments
Post a Comment