Trapped in pipe

Hi

Is there any way to find out in a single step ( command) the step where the pipe command failed when using multiple commands using pipe .

eg : ll *.tar | grep dec | grep december.tar

the first step is listing all tar files . Second step constitutes piping that data and

doing grep for dec . The q/p of this cmd is fed to 3rd pipe to get the desired

result .

Now what i want to know is that in case in any of the pipe steps the command fails ( suppose it doesnt return any o/p) .. which is the step .

Pls help

Hi.

You can try:

echo ${PIPESTATUS
[*]}

to see the result of each command in the pipe (if not the command - but it's quite obvious what it means):

$ echo blah | mkdir a/b/c | xargs echo
mkdir: cannot create directory `a/b/c': No such file or directory

bash: echo: write error: Broken pipe
$ echo ${PIPESTATUS
[*]}
1 1 0

To get the exit code of the last command in the pipe to fail only, use

$ set -o pipefail
$ mkdir x/y/y | xargs echo
mkdir: cannot create directory `x/y/y': No such file or directory
$echo $?
1

$ set +o pipefail
$ mkdir x/y/y | xargs echo
mkdir: cannot create directory `x/y/y': No such file or directory
$ echo $?
0

Edit: Just had the realisation that this is BASH only. This cfajohnson link shows how to do this in a POSIX way.

Why did I get 0 1 0?

$  echo blah | mkdir a/b/c | xargs echo
mkdir: cannot create directory `a/b/c': No such file or directory

$ echo ${PIPESTATUS[*]}
0 1 0
$

Hi.

Different OS, different versions, different handling, perhaps?

OS           Bash      P1 P2 P3
AIX 5.3:     2.5       1  1  0
CentOS 5.4:  3.2.5     0  1  0
Solaris 10:  3.0.0.16  0  2  0
FreeBSD:     3.2.48    0  1  0

The example was only demonstrative in any case.

For completeness, ksh93g and up also have this option..

thanks everybody for ur suggestions ...

I learnt anew thing :slight_smile: