0

I have a pipeline like so:

  tail -n0 -f  "${my_input}" | ql_receiver_lock_holder | while read line; do
   echo "$line" >> "${my_output}";
   # xxx: how can I programmatically close the pipeline at this juncture?
  done & disown;

my question: is there a way to programmatically close the pipeline where it says xxx? I could probably just call exit 0; but I am wondering if there is a way to close the current pipeline somehow.

  • 2
    What effect do you want to achieve here? Read a single line of input? If so, why use while at all? ql_receiver_lock_holder | (read line; echo ...) & disown would work just as well for that. Or do you want to end the loop? In which case a simple break would do. – muru Apr 23 '18 at 6:04
  • For me this sound like you want to break out of the loop, but it is really difficult to understand what you want to archive. – Raphael Ahrens Apr 24 '18 at 7:21
2

In:

tail -n0 -f -- "$my_input" |
  ql_receiver_lock_holder |
  sed /xxx/q > "$my_output"
  • sed would exit after reading the first line containing xxx.
  • ql_receiver_lock_holder would then exit (killed by a SIGPIPE) upon the first write it does to stdout (the now broken pipe) after that.
  • Likewise, tail would exit upon the first write it does after that.

If you want ql_receiver_lock_holder and tail to exit as soon as sed exits without waiting for their next write to stdout, you can use approaches described at

Note that this kind of while read loop is not the right way to process text in shells. At the very least, you'd need something like:

while IFS= read -r line; do
  printf '%s\n' "$line"
  case $line in
    (*xxx*) break
  esac
done

to replace the sed /xxx/q but which would be terribly inefficient except for very small input.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.