Bash - Use automatic file descriptor creation instead of fifo

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question






















  • 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














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.







share|improve this question






















  • 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












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.







share|improve this question














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.









share|improve this question













share|improve this question




share|improve this question








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
















  • 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










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





share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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





    share|improve this answer


























      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





      share|improve this answer
























        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





        share|improve this answer














        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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 18 '17 at 21:33

























        answered Nov 18 '17 at 19:49









        meuh

        29.6k11751




        29.6k11751



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?