Shell – Howto create a permanent client connection with netcat

io-redirectionnetcatshell

I'm writing a bash script that constantly read a folder in a loop to send data to a server at a defined time interval. I'm using netcat as the tool to connect to the server and send the data. My pseudo code would look like:

while true
do
    read_folder()
    process_data() > result.txt
    cat result.txt > netcat ip port
    wait 10 sec
done

My only problem is that in the scenario the client connects and disconnects the TCP/IP connection to the server every time. I would prefer to establish the connection at the start of the script and close it at the end.

Is there any way this could be done with command line tools in a bash script?

Best Answer

Two separate processes: One that copies result.txt to netcat. Result.txt is fed via another process.

echo -n >result.txt
tail -f result.txt | nc ip port &
while true
do
    read_folder()
    process_data() > result.txt
    wait 10 sec
done
Related Question