客户端使用coproc不会退出。

huangapple go评论62阅读模式
英文:

client using coproc does not exit

问题

我有一个像这样的过滤器:

#!/bin/bash

while
read line
do
echo $line | tr "B" "b"
done

我有一个客户端使用coproc来使用该过滤器,像这样:

#!/bin/bash

coproc ./filter
echo Bogus >&"${COPROC[1]}"
cat <&"${COPROC[0]}"

现在客户端不会退出。我必须使用Ctrl + C来退出。

我希望客户端在达到最后一行时退出。

如何实现这一点?

英文:

I have a filter like:

#!/bin/bash

while
read line
do
echo $line | tr &quot;B&quot; &quot;b&quot;
done

I have a client that use that filter using coproc like:

#!/bin/bash

coproc ./filter
echo Bogus &gt;&amp;&quot;${COPROC[1]}&quot;
cat &lt;&amp;&quot;${COPROC[0]}&quot;

Now the client does not exit. I have to use <kbd>Ctrl + C</kbd> to exit.

I want the client to exit when it reach the last line.

How to achieve that?

答案1

得分: 3

如果你不想再发送任何内容,通过关闭文件描述符来通知另一方。

coproc ./filter
echo 伪造 &gt;&amp;&quot;${COPROC[1]}&quot;
exec {COPROC[1]}&gt;&amp;-
cat &lt;&amp;&quot;${COPROC[0]}&quot;

这样,read line 将以失败退出,因此 while 将中断,过滤脚本也会退出。

英文:

If you do not want to send anything more inform the other side about it by closing the file descriptor.

coproc ./filter
echo Bogus &gt;&amp;&quot;${COPROC[1]}&quot;
exec {COPROC[1]}&gt;&amp;-
cat &lt;&amp;&quot;${COPROC[0]}&quot;

That way, read line will exit with failure, so while will break and filter script will exit.

huangapple
  • 本文由 发表于 2023年7月27日 22:37:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定