Skip to content
Advertisement

How to split given text into 3 variables in bash and send to host at port?

I want to write a command that listens to some port (say port 22222), receives a single line of text, and splits that line by spaces into port, host, and text by spaces. The text can have spaces in it too. So, for example, 1234 localhost blah de blah would be be split into 1234 for the port, localhost for the host, and blah de blah for the text. If the port is 0, the program exits. Otherwise the program sends the value of text to the host at the port and loops back to listening.

So I have in the terminal:

JavaScript

and

JavaScript

will cause blah blah to be sent to localhost at port 2016 and

JavaScript

will cause the program to exit

My question is what exactly goes into the “some code goes here I think” section?

Advertisement

Answer

Upon receive nc will echo the data to stdout, so you can process it as follows:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement