On Monday, February 13, 2012 11:35:04 PM Aaron Davies wrote:
> 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 haven't found anything)

As far as I'm aware, ksh has no PIPESTATUS equivalent. It has a pipefail but 
that won't help here. Even if it did, pipelines are executed in parallel, so 
there's no concept of conditionally executing only part of a pipeline since it 
would require waiting for a previous command's status to become available. I 
suppose you could rig together some complex configuration of traps, and test 
exit statuses of each part of the pipeline, then signal one of the subshells 
or the whole pipeline to exit when a failure occurs. There are a lot of 
possibilitie solutions, but any system like that will always be subject to 
race conditions.

Judging by the UUOC in your example, you may have more important things to 
figure out than how to do this. The correct way to handle that example would be 
either:

[[ -f foo ]] && wc -l foo

Or simply "wc -l foo" and check the exit status of wc. The only shell with any 
sort of try/catch is zsh, but that's unnecessary for something so simple.

-- 
Dan Douglas

Attachment: signature.asc
Description: This is a digitally signed message part.

_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users

Reply via email to