Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to devise a one-liner to send text to a simple echo server - but multiple connections in parallel. This is what I'm trying:

echo -e  A,B,C,D | xargs -d, -i -P 4 echo {} |  nc localhost 7000

However, it doesn't quite work. The server receives four connections but no data ("null").

What is the correct xargs and netcat command here?

share|improve this question

3 Answers 3

The answer is to use sh -c to launch xargs:

echo 'A,B,C,D' | xargs -d, -i -P 4 sh -c 'echo "{}" | nc localhost 7000'

See: http://stackoverflow.com/questions/845863/how-to-use-in-an-xargs-command

share|improve this answer

Use echo -n or you will get 5 connections instead four, one of them with no data. If in the server this is not checked, maybe it is causing your problem.

echo -n A,B,C,D | xargs -d, -I{} -P 4 -n 1 echo {} | nc localhost 7000

Moreover, using a netcat as listening server, it appears to work right:

zhen@sydow:~$ nc -l 7000
A
D
B
C

And also it seems to be parallel, I see out-of-order echo.

share|improve this answer

You don't need to repeat the echo, it is the default action of xargs. This one-liner serves as a quick self-contained test, but you can remove the test listener set up before the &

nc -l 7001 & echo  A B C D | xargs -P4 -n1 | nc localhost 7001

Results:

amit@deimos:~$ nc -l 7001 & echo A B C D | xargs -P4 -n1 | nc localhost 7001
[6] 7115
D
B
C
A
[6]   Done                    nc -l 7001
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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