更多的协处理器问题 [英] More coproc questions

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

问题描述

这是一个后续到 bash的协处理器以及吃剩的协处理器输出

我终于尘埃落定上在同一时间处理文件的一行成语是:

The idiom I finally settled on for processing a file one line at a time is:

coproc cat auto/etc/build.cfg
while read -u ${COPROC[0]} BRANCH TARGET SVNSRC SVNTAG BUILDTYPE DISTTYPE DISTARGS
do
    ... commands ...
done

我知道猫的简单情况我可以只使用输入重定向。这是一个简化的版本,其中,真正的使用egrep的选择线的一个子集

I know for the simple case of cat I could just use input redirection. This is a simplified version, where the real one uses egrep to select a subset of lines.

可惜,这是行不通的。

$ cat test.sh
coproc cat auto/etc/build.cfg
while read -u ${COPROC[0]} BRANCH TARGET SVNSRC SVNTAG BUILDTYPE DISTTYPE DISTARGS
do
    echo hello
done

$ bash -x test.sh
+ read -u 63 BRANCH TARGET SVNSRC SVNTAG BUILDTYPE DISTTYPE DISTARGS
+ cat auto/etc/build.cfg
+ echo hello
hello
+ read -u BRANCH TARGET SVNSRC SVNTAG BUILDTYPE DISTTYPE DISTARGS
test.sh: line 2: read: BRANCH: invalid file descriptor specification

似乎,即使有4条线中的文件,由第二次迭代时的协进程已经消失,其文件描述符沿,并且 $ {协处理器[*]} 变量已成为未设置(注意在第二次读取丢失的FD)。

It appears that, even though there are 4 lines in the file, by the time of the second iteration the coprocess has vanished, along with its file descriptors, and the ${COPROC[*]} variable has become unset (note the missing fd in the second read).

要复杂的是,我在这两个Cygwin和Linux操作系统(Fedora的)工作,这表现出在两个环境稍有不同的行为。在Linux中,我得到上述行为。在Cygwin的一些命令(即回声,这是一个bash内建)不会导致此问题。在这两种环境中,在运行任何外部命令杀害协进程,并扔了描述。

To complicate matters, I'm working in both Cygwin and Linux (Fedora), and this exhibits slightly different behaviors in the two environments. In Linux I get the behavior described above. In Cygwin some commands (i.e. echo, which is a bash builtin) do not cause this problem. In both environments, running any external command kills the coprocess and tosses its descriptors.

我想在这里真正的问题是:

I guess the real question here is:

在bash中,我怎么完成

In bash, how do I accomplish

while read (list of vars) from file
do
    one or more commands
done

该配置文件很短,所以如果我拥有对整个文件加载到一个数组,这将正常工作。

The config files are short, so if I have to load the entire file into an array, that will work.

我真的很惊讶,这是这么难在bash。

I'm really surprised that this is so hard in bash.

推荐答案

我不知道为什么你的协处理器的使用不工作。你的榜样为我工作用的庆典的4.2.8在Mac OS X的也许某些版本的庆典的是越野车在这里。

I am not sure why your usage of coproc is not working. Your example works for me with bash 4.2.8 on Mac OS X. Maybe certain versions of bash are buggy here.

听起来你可以从一个进程替换使用重定向一个协代替。

It sounds like you could redirect from a process substitution instead of using a coprocess.

while read foo bar baz quux; do
    : use foo, bar, baz, quux in various commands
done < <(commands | that | generate --your lines-to-read)

这是很方便的,当你的输入是不是已经在一个简单的文件,你可以使用普通的重定向。

This can be handy when your input is not already in a simple file where you could use plain redirection.

如果你想坚持用协处理器,你也许可以使用替代配方:

If you want to stick with coproc, you might be able to use an alternate formulation:

coproc { commands | that | generate --your lines-to-read ; }
while read foo bar baz quux; do
    : use foo, bar, baz, quux in various commands
done <&${COPROC[0]}

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

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