Re: Redirect a backgrounded process' stdout toward COPROC's stdin
On 6/3/12 8:20 AM, Davide Baldini wrote: Machine: i486 OS: linux-gnu Compiler: gcc Compilation CFLAGS: -O2 -march='i486' (plus the flags added by Makefile) uname output: Linux debianBunker 2.6.26-2-686 #1 SMP Wed Sep 21 04:35:47 UTC 2011 i686 GNU/Linux Machine Type: i486-pc-linux-gnu Bash Version: 4.2.0(5)-release Patch Level: 5 Release Status: release Description: In the following test script I run an elementary coprocess to which the echo built-in, run in background, attaches its standard-output: #!/bin/bash # TEST 1 coproc /bin/sleep 100 echo ${COPROC[1]} The script always fails, for no apparent reason, giving the output: ./test.sh: line 4: ${COPROC[1]}: Bad file descriptor Coproc file descriptors are not available to subshells. They're implemented using pipes, and leaving pipe file descriptors open in subshells causes processes to hang and not terminate properly, which results in very hard-to-track-down-and-reproduce bugs. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
Redirect a backgrounded process' stdout toward COPROC's stdin
Machine: i486 OS: linux-gnu Compiler: gcc Compilation CFLAGS: -O2 -march='i486' (plus the flags added by Makefile) uname output: Linux debianBunker 2.6.26-2-686 #1 SMP Wed Sep 21 04:35:47 UTC 2011 i686 GNU/Linux Machine Type: i486-pc-linux-gnu Bash Version: 4.2.0(5)-release Patch Level: 5 Release Status: release Description: In the following test script I run an elementary coprocess to which the echo built-in, run in background, attaches its standard-output: #!/bin/bash # TEST 1 coproc /bin/sleep 100 echo ${COPROC[1]} The script always fails, for no apparent reason, giving the output: ./test.sh: line 4: ${COPROC[1]}: Bad file descriptor I wonder if the correct syntax should be rather this one (ampersand moved before redirection): #!/bin/bash # TEST 2 coproc /bin/sleep 100 echo ${COPROC[1]} This second example seems to work since it reports no errors during execution, but with this syntax, the redirection is not performed in practice; in fact, consider this other test: #!/bin/bash # TEST 3 /bin/echo abc xfile Test 3 creates the file xfile, but does not write anything into it. Curiously, trying again to position the ampersand after the redirection make the echo work fine: #!/bin/bash # TEST 4 /bin/echo abc xfile Test 4 creates the file xfile with inside the string abc. What's wrong with the coproc? And what's the correct position for the ampersand, before redirection, or after?
Re: Redirect a backgrounded process' stdout toward COPROC's stdin
2012/6/3 Davide Baldini baldiniebald...@gmail.com: Description: In the following test script I run an elementary coprocess to which the echo built-in, run in background, attaches its standard-output: #!/bin/bash # TEST 1 coproc /bin/sleep 100 echo ${COPROC[1]} The script always fails, for no apparent reason, giving the output: ./test.sh: line 4: ${COPROC[1]}: Bad file descriptor The coproc fds are only available in the same shell. The subshell created with cannot use them. I wonder if the correct syntax should be rather this one (ampersand moved before redirection): #!/bin/bash # TEST 2 coproc /bin/sleep 100 echo ${COPROC[1]} This is equivalent to echo ${COPROC[1]} ends the command, so the redirection is not applied to the echo. See http://wiki.bash-hackers.org/syntax/keywords/coproc -- Geir Hauge
Re: Redirect a backgrounded process' stdout toward COPROC's stdin
On 06/03/12 17:01, Geir Hauge wrote: 2012/6/3 Davide Baldini baldiniebald...@gmail.com: Description: In the following test script I run an elementary coprocess to which the echo built-in, run in background, attaches its standard-output: #!/bin/bash # TEST 1 coproc /bin/sleep 100 echo ${COPROC[1]} The script always fails, for no apparent reason, giving the output: ./test.sh: line 4: ${COPROC[1]}: Bad file descriptor The coproc fds are only available in the same shell. The subshell created with cannot use them. I wonder if the correct syntax should be rather this one (ampersand moved before redirection): #!/bin/bash # TEST 2 coproc /bin/sleep 100 echo ${COPROC[1]} This is equivalent to echo ${COPROC[1]} ends the command, so the redirection is not applied to the echo. See http://wiki.bash-hackers.org/syntax/keywords/coproc Thank you Geir, I really appreciated you help and the useful bash-hackers.org link. Definitely, the GNU Bash manual would need some deeper details on these niche features.