netcat and tee redirection via a bash script

2

Quick question that has been haunting me all day.. I have a bash script that includes this line:

nc -l 8044 | tee nc-output &

The output from netcat is never being written to nc-output. If I run that line outside of the bash script, it works.

nc -l 8044 > afile &

Also does not work, but works outside a bash script.

I need the ampersand there so I can run the process separately and continuing executing my batch script. Any suggestions would be great!

Thanks!

Gregorio Di Stefano

Posted 2012-06-19T16:25:34.733

Reputation: 220

One thing that comes to mind: does your script have a different working directory than you expect? – Mikey Boldt – 2012-06-19T17:02:52.507

I don't think that is the problem. The script is creating an empty file on each run.. just nothing written to it. – Gregorio Di Stefano – 2012-06-19T17:04:09.587

Answers

2

The problem is that as soon as nc accepts a connection it will try to read from stdin. When you run this as a bash script it will always read EOF immediately and close the connection, what doesn't happen when you run it outside a script.

Use -d switch to prevent nc from read from stdin.

nc -d -l 8044 > afile &

fmanco

Posted 2012-06-19T16:25:34.733

Reputation: 2 287

I will give this a try get back to you! Thanks! – Gregorio Di Stefano – 2012-06-19T17:35:41.157