Subject: Re: [ast-users] aborting a pipeline?
--------
> what's the best way to abort a pipeline partway through?
>
> e.g. in a simple case,
>
> $ ls foo
> ls: foo: No such file or directory
> $ cat foo|wc -l
> cat: foo: No such file or directory
> 0
> $
>
> i'd prefer it stopped after the "cat foo" failed
>
> (i'm sure this is a common request, but i've tried googling for solutions and
> ha
> ven't found anything)
> --
> Aaron Davies
> [email protected]
>
Here is how it can be done if the failure is in the first component
of a pipeline:
set -o monitor # causes each pipeline to be a process group
{ trap "exec ksh -c 'kill -- -$$'" ERR;first_command;} | ...|
last_command
If first_command fails, then a kill signal will be sent to the group
of the first process which should kill all the other commands.
Note, that if you define the function
ok()
{
trap "exec ksh -c 'kill -- -$$'" ERR
"$@"
}
Then
set -o monitor
ok false | sleep 3
should terminate right away.
To handle errors in any command in a pipeline, not just the first,
you would need to add a command mypgid to output the current process group and
modify the trap line to
trap 'kill -- -$(mypgid)' ERR
David Korn
[email protected]
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users