1

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?

Ahmad Ismail
  • 11,636
  • 6
  • 52
  • 87
  • 1
    This bash code will alter whitespace too, among other possible transformations (try sending the string `"*"` to the filter). use `IFS= read -r line` and `printf '%s\n' "$line" | tr 'B' 'b'` or `tr 'B' 'b' <<< "$line"` (or don't use tr: `printf '%s\n' "${line//B/b}"`) – glenn jackman Jul 27 '23 at 14:58
  • Is there a specific reason why you used a coproc? Looks like a simple pipe could be easier. If you want to pipe the output of many commands you can group them with `{ echo a; echo b; echo c; } | filter` (the commands do not need to be on one line) – Paul Pazderski Jul 27 '23 at 16:06

1 Answers1

3

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.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111