Best way to use Unix domain socket in bash script
I am working on a simple bash script demonstrator that uses Unix domain sockets. I have a loop like this:
#!/bin/bash
while true
do
rm /var/run/mysock.sock
command=`nc -Ul /var/run/mysock.sock`
echo $command > /tmp/command
done
I am repeating the / tmp / command command for debugging purposes only.
Is this the best way to do it?
+3
source to share
1 answer
Looks like I'm late for a party. Anyway, here's my suggestion, which I successfully use for one-time reply messages:
INPUT=$(mktemp -u)
mkfifo -m 600 "$INPUT"
OUTPUT=$(mktemp -u)
mkfifo -m 600 "$OUTPUT"
(cat "$INPUT" | nc -U "$SKT_PATH" > "$OUTPUT") &
NCPID=$!
exec 4>"$INPUT"
exec 5<"$OUTPUT"
echo "$POST_LINE" >&4
read -u 5 -r RESPONSE;
echo "Response: '$RESPONSE'"
Here I am using two FIFOs to talk to nc (1)
and get his answer.
+4
source to share