I'm trying to get the following code to read from a main input then read that in one function then be able to send that into the input of another function but i am having trouble getting it to work and it NEEDS to be inputted into the read command to be able to be parsed

coproc test {
for i in $(seq 0 9)
        sleep 1
        echo $i
input() { while read -u 3 gr
        echo sent: $gr # this should send to output function
output() { while read idk
        echo received: $idk | sed s/r/R/ | sed 's/5/five/' # this should receive from input function
exec 3>&${test[0]}
exec 4>&${test[1]}
input <&3 &>4 & # remove >&4 to see output
export input_PID=$!
output <&4 &
sleep 11
exec 3>&- 4>&-
echo finished

I've tried every kind of redirection of the fd but nothing seems to work, please help, the idea is that a user will be able to (based on what the output function reads) send commands into the input function without needing to duplicate the code for each function, and vice versa (input function can send to output function as well), sending to fd3 or fd4 doesn't work as it seems to bypass read itself and send directly to the command that is receiving the final output

Note that this is just a minimal example, the full use is here:

Basic process layout of the script (each indent represents the source depth or sub process depth):

    . ./modules/module_controller
        . ./modules/module_trap
            . ./modules/module_finish
        until [[ $FIN == 1 ]] ; do
        . ./modules/module_loader
            . ./modules/module_colors
            . ./modules/module_tracker
            . ./modules/module_config
            . ./modules/module_kill_all_panes
            . ./modules/module_irc_session
                . ./modules/module_input
            . ./modules/module_irc_read
                . ./modules/module_output
                . ./modules/module_handler
                . ./modules/module_user_input
            . ./modules/module_array
            if [[ -z $IRC_NC_PID && $IRC_FIN == "0" ]] ; then
            . ./modules/module_coproc_nc
            . ./modules/module_rest_of_script
            . ./modules/module_null

https://github.com/mgood7123/UPM/blob/master/Files/Code/modules/module_loader (this is continuously executed)


https://github.com/mgood7123/UPM/blob/master/Files/Code/modules/module_irc_read (core module ran in separate background process)

https://github.com/mgood7123/UPM/blob/master/Files/Code/modules/module_irc_session (core module ran in separate background process)

https://github.com/mgood7123/UPM/blob/master/Files/Code/modules/module_rest_of_script (THIS RUNS THE BACKGROUND FUNCTIONS FOR THE IRC_READ AND IRC_SESSION)

  • 1
    Your code and description are incomplete. What is ${test[0]}? Statements like "iv tried every kind of redirection of the fd but nothing seems to work" are generally not very useful. Put in the question what you have tried. – Hauke Laging Sep 17 '17 at 13:10
  • 2
    @HaukeLaging ${test[0]} is the coproc output but, indeed, OP has so much "tried every kind of redirection of the fd" that the current script does not make any sense at all, besides being completely buggy. It's hard to understand what you want to achieve Clark. – xhienne Sep 17 '17 at 13:57
  • Clark, If following our answers, you have further questions, do not change your original question drastically as you did, as it invalidates our answers (I have reverted your edits). Just open a new question. You may link the two questions in their description. – xhienne Sep 17 '17 at 17:20

The problem is that you mix up input and output:

exec 3>&${test[0]}
exec 4>&${test[1]}
input <&3 &>4 &

You open both &3 and &4 for writing only. But the first can only read. Reading from &3 fails because you have opened the fd for writing only. What you need is

exec 3<&${test[0]}
  • ok, ill try that – Clark Kent Sep 17 '17 at 15:15
  • i get line 13: read: read error: 4: Bad file descriptor when i try read -u 4 idk, and changing to 3<&${test[0]} doesnt seem to work – Clark Kent Sep 17 '17 at 15:23
  • @ClarkKent You have to read from &3 i.e. it must be read -u 3. You can only write to &4. – Hauke Laging Sep 17 '17 at 15:35

First of all, what cannot work in your script:

exec 3>&${test[0]}

As the coproc output, ${test[0]} is a file descriptor meant to be read from, not written to. Assuming you mean 3<&${test[0]}

input <&3 &>4 &

First, &>4 means "redirect stdout and stderr to a file named 4", you probably mean >&4. Now, FD #4 being the input of the test coproc (which is not read by test BTW) that redirection makes little sense.

output <&4 &

If there wasn't a typo, FD #4 would be an output file descriptor (bound to the coproc's stdin), you cannot read from it.

Now I'll try to give you a working code, assuming you want:

  • one background process input that reads from FD #3 and sends everything to output with "sent" prepended to each line.
  • another background process output that reads whatever input sends, prepends "received", does some string processing and writes the result on stdout

Normally, this would be as simple as this:

test() for i in {0..9}; do sleep 1; echo $i; done    # No coproc

input() while read gr; do echo sent: $gr; done       # Reads from stdin, not FD #3

# output() unmodified

exec 3> >(input | output)
test >&3

But I'm under the impression that, for whatever reason, you want to spawn input and output separately and put test in background. Then try this:

exec 4> >(output)

exec 3> >(input >&4)
exec 4>&-

test >&3 &
  • ill try that aswell – Clark Kent Sep 17 '17 at 15:17
  • @ClarkKent Remove -u 3 from your read command for this to work, I assumed input was reading from stdin. – xhienne Sep 17 '17 at 15:21
  • i get no output when i do exec 3> >(input | output) ; test >&3 even when i remove -u 3 – Clark Kent Sep 17 '17 at 15:26
  • both of the read commands to not need to have -u right? – Clark Kent Sep 17 '17 at 15:29
  • it outputs nothing no matter if i do read -u 3 or read -u 4 for either of the functions ;-; – Clark Kent Sep 17 '17 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.