Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 2417  / 2 Years ago, mon, october 17, 2022, 10:09:34

I can't get the following bash script to stay open after the first message is received from NC:



#!/bin/bash
port=3333
nc -l $port | while read msg; do notify-send Alert "$msg"; done


After the first message it exits. I want it to stay open and continue monitoring for new messages from NC.



I know that if I launch nc -l port without the while loop it stays open and I can chat away between the two connections even disconnect from the connected host.



I am sending the message using:



echo 'done' | nc IP port

More From » networking

 Answers
6

Just add a -k option to nc. Like this:



nc -l $port -k | while read msg; do notify-send Alert "$msg"; done



As seen in man nc:




-k
Forces nc to stay listening for another connection after its current connection is completed. It is an error to use this option without the -l option.



[#33722] Wednesday, October 19, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
byishted

Total Points: 469
Total Questions: 113
Total Answers: 113

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
;