MLUG: [MLUG] Re: basic Unix pipe/exit code problem
[MLUG] Re: basic Unix pipe/exit code problem
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Scott Hussey wrote:
> Google "PIPESTATUS" or search for it in the bash man page. 
> 
>        PIPESTATUS
>               An  array  variable (see Arrays below) containing a list
> of exit
>               status values from the processes in  the  most-recently-executed
>               foreground pipeline (which may contain only a single command).

Mike Miller wrote:
> There it is!  So you can do things like this in a script:
> 
> #!/usr/bin/bash
> false | sort | uniq | grep 1 | gzip | > /dev/null
> echo ${PIPESTATUS[*]}
> 
> and it will return this:
> 
> 1 0 0 1 0 0

Mark Rages wrote:
> Yes, that would work, but I ended up using subshells like this:
> 
> trap 'exit 1' 15
> 
> (A || kill $$) | 
>    (B || kill $$) | 
>           ...
> 
> I wrote this Python function to assemble the above sequence for me:
> 
> def safe_pipe_system(commands):
>     """ constructs a bash system of pipes, where a failure in any pipe will
>         result in the system failing.
>  
>         commands is a list of commands in the order to be piped together.
>         return value is a string, ready to pass to os.system() etc.
>         """
>          
>     commands=['']+[command+' || kill $$ ' for command in commands]
>  
>     return ("trap 'exit 1' 15\n"+
>             reduce(lambda a,b: '('+a+b+') | ',
>                    commands[:-1])
>             +commands[-1])

Two interesting solutions. And the PIPESTATUS feature of bash was news to me.
I learn something new every day. Cool beans.

Mike/
_______________________________________________
members mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/members