Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: kill the LHS command of a pipe once the RHS command terminates



2019-06-28 13:04:30 +0200, Vincent Lefevre:
> With some commands I pipe the output to a pager, and when quitting
> the pager, I want the command to terminate immediately (so that it
> doesn't consume more resources) and want the shell prompt back.
[...]

See also:

https://unix.stackexchange.com/questions/404272/how-to-exit-early-on-pipe-close?noredirect=1&lq=1
https://unix.stackexchange.com/questions/366797/grep-slow-to-exit-after-finding-match/366806#366806
https://unix.stackexchange.com/questions/416150/make-tail-f-exit-on-a-broken-pipe?noredirect=1&lq=1

So here with zsh, you can do:

zmodload zsh/system

page-and-kill-cmd() {
  (echo $sysparams[pid]; "$@") | (
    IFS= read -r pid
    ${PAGER:-less}
    kill -s PIPE $pid
  )
}

And run:

page-and-kill-cmd cmd args

For a complex command, use eval:

page-and-kill-cmd eval 'seq 100; sleep 10; seq 10'

That won't work in all the cases as we're only killing one pid.

-- 
Stephane



Messages sorted by: Reverse Date, Date, Thread, Author