0

I have a server outputting some numbers, I'm reading them with socat, then using sed, I turn those numbers into some math equations, I run them with bc, then I need to pipe them into xargs.

socat -u udp-recv:10552,reuseaddr,crnl - | sed -u s/.*/1+1/ | bc | xargs echo

The pipe above produces no output. My pipe is exactly like this except for the sed statement, and it also produces no output.

yes | gsed -u s/.*/1+1/ | bc | xargs echo

A pipe such as the above, however, prints a bunch of 2s to the console as expected. Both pipes should be the same though, because the sed line changes everything into the same phrase no matter what. Why are does the pipe with socat produce no output? My only guess is that the server only sends numbers once a second, and there's some kind of buffer problem, but using stdbuf -o0 does not change anything.

1

Breaking down the components:

# From host1 - Start with 'socat'
user@host1:~# socat -u udp-recv:10552,reuseaddr,crnl -
# From host2 send some data via udp to host1:10552
user@host2:~# echo foo > /dev/udp/host1/10552
# Result on host1
foo

# From host1 - Add 'sed'
user@host1:~# socat -u udp-recv:10552,reuseaddr,crnl - | sed -u s/.*/1+1/
# From host2 send some data via udp to host1:10552
user@host2:~# echo foo > /dev/udp/host1/10552
# Result on host1
1+1

# From host1 - add 'bc'
user@host1:~# socat -u udp-recv:10552,reuseaddr,crnl - | sed -u s/.*/1+1/ | bc
# From host2 send some data via udp to host1:10552
user@host2:~# echo foo > /dev/udp/host1/10552
# Result on host1
2

# From host1 - add 'xargs echo'
user@host1:~# socat -u udp-recv:10552,reuseaddr,crnl - | sed -u s/.*/1+1/ | bc | xargs echo
# From host2 send some data via udp to host1:10552
user@host2:~# echo foo > /dev/udp/host1/10552
# Result on host1
[ nothing ]

What's missing? A placeholder. Note: '-i' enables replacement strings, and '{}' is the placeholder.

# From host1 - setup xargs with -i
user@host1:~# socat -u udp-recv:10552,reuseaddr,crnl - | sed -u s/.*/1+1/ | bc | xargs echo -i{} echo {}
# From host2 send some data via udp to host1:10552
user@host2:~# echo foo > /dev/udp/host1/10552
# Result on host1
2

Hmm. Seems -i is depricated. Use -I.

user@host1:~# socat -u udp-recv:10552,reuseaddr,crnl - | sed -u s/.*/1+1/ | bc | xargs echo -I{} echo {}

OR use '-n1' to set max-args to 1.

socat -u udp-recv:10552,reuseaddr,crnl - | sed -u s/.*/1+1/ | bc | xargs -n1

OR use '-L1' to set max-lines to 1. As mentioned in the comments.

socat -u udp-recv:10552,reuseaddr,crnl - | sed -u s/.*/1+1/ | bc | xargs -L1

More on complex commands:

http://www.tldp.org/LDP/abs/html/moreadv.html

Hopefully this helps.

  • In summary: xargs is waiting for more input. I think that xargs -L 1 is probably sufficient to avoid that. – Toby Speight May 29 '15 at 8:50
  • Yea, I came back to add the -n option, but your comment may be more valid depending what's gets passed, and who's reading this answer. Thanks. @TobySpeight – Alex Atkinson May 29 '15 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.