后台处理重定向到协处理器 [英] Background process redirect to COPROC

查看:155
本文介绍了后台处理重定向到协处理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的测试脚本我运行一个基本的协进程到了回声内置的,在后台运行,高度重视其标准输出:

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

测试3创建文件 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 &

测试4创建文件 xfile 与字符串内 ABC

有什么原因造成的协处理器重定向错误或者什么的正确语法是一些想法?

Have some idea on what is causing the coproc redirection error or what the correct syntax is?

推荐答案

您已经得到了其他答案的 http://lists.gnu.org/archive/html/bug-bash/2012-06/msg00027.html

You've got the answer elsewhere http://lists.gnu.org/archive/html/bug-bash/2012-06/msg00027.html:

协处理器文件描述符不提供给子shell。他们是
  使用管道来实现,并留下管文件描述符打开
  子shell进程会导致挂起,并不会终止正确,这
  结果非常难以跟踪下和重现bug。

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.

这篇关于后台处理重定向到协处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆