At the beginning, I created a small server with netcat and it worked well:
#!/bin/bash
PORT="1234";
startServer(){
fifo="fifo/$1";
mkfifo "$fifo";
connected="0";
netcat -q 0 -l -p "$PORT" < "$fifo" | while read -r line; do
if [ "$connected" == "0" ];then
#listen for a new connection
startServer $(($1+1))&
connected="1";
fi
#server logic goes here
done > "$fifo";
rm "$fifo";
}
mkdir fifo;
startServer 0;
It uses fifos to redirect the main loop output back to netcat.
Every call to startServer increases the value of $1 and creates a fifo file fifo/$1.
But I want to use bash's automatic file descriptor creation to get rid of fifo files and the parameter of startServer.
After multiple attempts, here is my current attempt at modifying the startServer function. I can receive lines from the client but unfortunately it does not receive anything back. I am confused about what is wrong.
startServer(){
connected="0";
#create file descriptor and read output from main loop
exec {fd}> >( netcat -q 0 -l -p "$PORT" | while read -r line; do
if [ "$connected" == "0" ];then
#listen for a new connection
startServer&
connected="1";
fi
#server logic goes here
done ) >&$fd;
#close file descriptor
exec {fd}<&-;
exec {fd}>&-;
}
Also, I cannot use netcat's "-e" option since it is not available in the netcat-openbsd package.