I am trying to create a persistent connection using bash. On terminal 1, I keep netcat as the server:
$ nc -vlkp 3000 Listening on [0.0.0.0] (family 0, port 3000)
On terminal 2, I create fifo and save cat:
$ mkfifo fifo $ cat > fifo
On terminal 3, I make fifo as a client netcat login:
$ cat fifo | nc -v localhost 3000 Connection to localhost 3000 port [tcp/*] succeeded!
On terminal 4, I send everything I want:
$ echo command1 > fifo $ echo command2 > fifo $ echo command3 > fifo
Returning to terminal 1, I see the commands received:
$ nc -vlkp 3000 Listening on [0.0.0.0] (family 0, port 3000) Connection from [127.0.0.1] port 3000 [tcp
So everything works. But when I put this in a script (I called fifo.sh), bash cannot write to fifo:
On terminal 1, the same listening server:
$ nc -vlkp 3000 Listening on [0.0.0.0] (family 0, port 3000)
On terminal 2, I ran a script:
#!/bin/bash rm -f fifo mkfifo fifo cat > fifo & pid1=$! cat fifo | nc -v localhost 3000 & pid2=$! echo sending... echo comando1 > fifo echo comando2 > fifo echo comando3 > fifo kill -9 $pid1 $pid2
Output in terminal 2:
$ ./fifo.sh Connection to localhost 3000 port [tcp/*] succeeded! sending...
On terminal 1, I see only the connection. No commands:
$ nc -vlkp 3000 Listening on [0.0.0.0] (family 0, port 3000) Connection from [127.0.0.1] port 3000 [tcp
Any idea on why it only works interactively? Or is there another way to create a persistent connection using only Bash? I don't want to look for Expect, because I have a larger bash script that does some work after sending command1, and command2 depends on the output of command1, etc.
Thanks!