Search code examples
bashcoproc

client using coproc does not exit


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 Ctrl + C to exit.

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

How to achieve that?


Solution

  • 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.