英文:
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 "B" "b"
done
I have a client that use that filter using coproc like:
#!/bin/bash
coproc ./filter
echo Bogus >&"${COPROC[1]}"
cat <&"${COPROC[0]}"
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 伪造 >&"${COPROC[1]}"
exec {COPROC[1]}>&-
cat <&"${COPROC[0]}"
这样,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 >&"${COPROC[1]}"
exec {COPROC[1]}>&-
cat <&"${COPROC[0]}"
That way, read line
will exit with failure, so while
will break and filter script will exit.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论