Bash - Use automatic file descriptor creation instead of fifo
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
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"
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() 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.
bash file-descriptors netcat
add a comment |Â
up vote
4
down vote
favorite
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"
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() 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.
bash file-descriptors netcat
Allocating a file descriptor doesn't allocate a buffer for it to refer to.
â chepner
Nov 18 '17 at 18:42
@chepner The redirection >( ... ) or <( ... ) should allocate a buffer.
â nd97
Nov 18 '17 at 22:28
1
That's not a redirection; it's a process substitution, which is effectively just special syntax for a fifo.
â chepner
Nov 18 '17 at 22:40
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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"
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() 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.
bash file-descriptors netcat
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"
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() 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.
bash file-descriptors netcat
edited Nov 18 '17 at 15:31
asked Nov 18 '17 at 14:21
nd97
234
234
Allocating a file descriptor doesn't allocate a buffer for it to refer to.
â chepner
Nov 18 '17 at 18:42
@chepner The redirection >( ... ) or <( ... ) should allocate a buffer.
â nd97
Nov 18 '17 at 22:28
1
That's not a redirection; it's a process substitution, which is effectively just special syntax for a fifo.
â chepner
Nov 18 '17 at 22:40
add a comment |Â
Allocating a file descriptor doesn't allocate a buffer for it to refer to.
â chepner
Nov 18 '17 at 18:42
@chepner The redirection >( ... ) or <( ... ) should allocate a buffer.
â nd97
Nov 18 '17 at 22:28
1
That's not a redirection; it's a process substitution, which is effectively just special syntax for a fifo.
â chepner
Nov 18 '17 at 22:40
Allocating a file descriptor doesn't allocate a buffer for it to refer to.
â chepner
Nov 18 '17 at 18:42
Allocating a file descriptor doesn't allocate a buffer for it to refer to.
â chepner
Nov 18 '17 at 18:42
@chepner The redirection >( ... ) or <( ... ) should allocate a buffer.
â nd97
Nov 18 '17 at 22:28
@chepner The redirection >( ... ) or <( ... ) should allocate a buffer.
â nd97
Nov 18 '17 at 22:28
1
1
That's not a redirection; it's a process substitution, which is effectively just special syntax for a fifo.
â chepner
Nov 18 '17 at 22:40
That's not a redirection; it's a process substitution, which is effectively just special syntax for a fifo.
â chepner
Nov 18 '17 at 22:40
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
You cannot set and use fd
in the same command; you are effectively doing exec fd> ... >&$fd
. What can work is creating the bash fifo/pipe first, using some simple command like :
. Eg:
startServer()
local connected=0 fd
exec fd<> <(:)
nc -q 0 -l -p "$PORT" <&$fd
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You cannot set and use fd
in the same command; you are effectively doing exec fd> ... >&$fd
. What can work is creating the bash fifo/pipe first, using some simple command like :
. Eg:
startServer()
local connected=0 fd
exec fd<> <(:)
nc -q 0 -l -p "$PORT" <&$fd
add a comment |Â
up vote
3
down vote
accepted
You cannot set and use fd
in the same command; you are effectively doing exec fd> ... >&$fd
. What can work is creating the bash fifo/pipe first, using some simple command like :
. Eg:
startServer()
local connected=0 fd
exec fd<> <(:)
nc -q 0 -l -p "$PORT" <&$fd
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You cannot set and use fd
in the same command; you are effectively doing exec fd> ... >&$fd
. What can work is creating the bash fifo/pipe first, using some simple command like :
. Eg:
startServer()
local connected=0 fd
exec fd<> <(:)
nc -q 0 -l -p "$PORT" <&$fd
You cannot set and use fd
in the same command; you are effectively doing exec fd> ... >&$fd
. What can work is creating the bash fifo/pipe first, using some simple command like :
. Eg:
startServer()
local connected=0 fd
exec fd<> <(:)
nc -q 0 -l -p "$PORT" <&$fd
edited Nov 18 '17 at 21:33
answered Nov 18 '17 at 19:49
meuh
29.6k11751
29.6k11751
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f405439%2fbash-use-automatic-file-descriptor-creation-instead-of-fifo%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Allocating a file descriptor doesn't allocate a buffer for it to refer to.
â chepner
Nov 18 '17 at 18:42
@chepner The redirection >( ... ) or <( ... ) should allocate a buffer.
â nd97
Nov 18 '17 at 22:28
1
That's not a redirection; it's a process substitution, which is effectively just special syntax for a fifo.
â chepner
Nov 18 '17 at 22:40