0

I want to read/write to a unix socket in a bash script, but only do it with one connection. All of the examples I've seen using nc say to open different socket connections for every read/write.

Is there a way to do it using one connection throughout the script for every read/write?

(nc only lets me communicate with the socket in a one shot manner)

1 Answers1

0

Run the whole script with output redirected:

{
    command
    command
    command
} | nc -U /tmp/cppLLRBT-socket

However, pipes are one-way, so you can do this for reading or writing, but not both.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I can't... [aa@localhost cppB-tree]$ ls -l /tmp/cppLLRBT-socket srwxrwxr-x. 1 aa aa 0 Oct 26 19:20 /tmp/cppLLRBT-socket [aa@localhost cppB-tree]$ exec 3<>/tmp/cppLLRBT-socket bash: /tmp/cppLLRBT-socket: No such device or address It refuses to work for some reason – CourtneyAccorso Oct 26 '17 at 18:23
  • Sockets aren't accessed using the normal file `open()` function. How are you doing it for each read/write now? – Barmar Oct 26 '17 at 18:25
  • echo "blaa" | nc -U /tmp/cppLLRBT-socket – CourtneyAccorso Oct 26 '17 at 18:29