Sample Header Ad - 728x90

inter process comunication in bash read commands in multiple background functions

-3 votes
2 answers
505 views
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) do sleep 1 echo $i done } input() { while read -u 3 gr do echo sent: $gr # this should send to output function done } output() { while read idk do echo received: $idk | sed s/r/R/ | sed 's/5/five/' # this should receive from input function done } exec 3>&${test} exec 4>&${test} input 4 & # remove >&4 to see output export input_PID=$! output &- 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): Code_Bash . ./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 fi . ./modules/module_null done 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_coproc_nc 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)
Asked by Clark Kent (11 rep)
Sep 17, 2017, 12:58 PM
Last activity: Sep 17, 2017, 05:40 PM