Trapped in pipe


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trapped in pipe
# 1  
Old 01-12-2010
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
# 2  
Old 01-12-2010
Hi.

You can try:

Code:
echo ${PIPESTATUS[*]}

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

Code:
$ 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
Code:
$ 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.

Last edited by Scott; 01-12-2010 at 08:50 AM..
# 3  
Old 01-12-2010
Why did I get 0 1 0?

Code:
$  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
$

# 4  
Old 01-12-2010
Hi.

Different OS, different versions, different handling, perhaps?

Code:
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.
# 5  
Old 01-12-2010
Quote:
Originally Posted by scottn
[/code]Edit: Just had the realisation that this is BASH only .
For completeness, ksh93g and up also have this option..
# 6  
Old 01-18-2010
thanks everybody for ur suggestions ...

I learnt anew thing :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Signal trapped during read resumes sleeping

Greetings. This is my first post in this forum; I hope y'all find it useful. One caveat: "Concise" is my middle name. NOT! :D I am almost done with a shell script that runs as a daemon. It monitors a message log that is frequently written to by a database server but it it works my client will... (2 Replies)
Discussion started by: jakesalomon
2 Replies

2. Shell Programming and Scripting

How to ignore Pipe in Pipe delimited file?

Hi guys, I need to know how i can ignore Pipe '|' if Pipe is coming as a column in Pipe delimited file for eg: file 1: xx|yy|"xyz|zzz"|zzz|12... using below awk command awk 'BEGIN {FS=OFS="|" } print $3 i would get xyz But i want as : xyz|zzz to consider as whole column... (13 Replies)
Discussion started by: rohit_shinez
13 Replies

3. Shell Programming and Scripting

Replace pipe with Broken Pipe

Hi All , Is there any way to replace the pipe ( | ) with the broken pipe (0xA6) in unix (1 Reply)
Discussion started by: saj
1 Replies

4. Shell Programming and Scripting

How can I use pipe

Hi, guys: I am working on my shell using c. How can I use pipe to implement the following? ls -l 1>> | grep hellp 1<< 2>> | less 2<< (the output of ls goes to grep, and the output of grep goes to less) Thanks Please use and tags when posting code, data or logs etc. to preserve... (1 Reply)
Discussion started by: tomlee
1 Replies

5. Programming

pipe program in C

Hello guys, my professor give me 2 days to study and make a program usign pipe, fork in c i need to do a program read a arq.txt the father process read the file and the child print ! like this #include <stdio.h> #include <stdlib.h> #include <sys/wait.h> int main (){ ... (1 Reply)
Discussion started by: beandj
1 Replies

6. Shell Programming and Scripting

Breaking out of a pipe

I have the following command in a Bash shell script: who | grep -w $1 | some other commands If grep fails, an error message is displayed. How do I test if grep fails and still be able to pipe it's output to the rest of the commands? I have the following solution: a=`who | grep -w $1`... (3 Replies)
Discussion started by: oinkl
3 Replies

7. Shell Programming and Scripting

Non-blocking pipe

Hello, Would this be an acceptable way of creating a non-blocking pipe. Basically I want to create kind of a server client arch. This code would be in the server, and I don't want to have to wait for clients to read before moving on to the next client. One problem I can see is if... (4 Replies)
Discussion started by: cdlaforc
4 Replies

8. UNIX for Advanced & Expert Users

Trapped Signal HUP

We encountered an issue in our project while using the Interix UNIX (SFU 3.5) and explained our query below. We would be happy if anybody helps us to troubleshoot the problem J In our code the trapping signal for all signals like HUP, INT, QUIT, ILL, TRAP, ABRT, EXCEPT, etc., is initialized in... (4 Replies)
Discussion started by: RAMESHPRABUDASS
4 Replies

9. UNIX for Dummies Questions & Answers

function and pipe

Hi, Is it possible that the output of a command is piped into a unix function? Just like in below: #!/bin/ksh concat(){ echo Orbix } echo "Hello there" | concat How to manipulate the output of a command inside the function? (2 Replies)
Discussion started by: Orbix
2 Replies

10. Programming

pipe help

i made a lot of processes. here is the code: main() { printf("\nEnter K="); scanf("%d",&k); printf("Enter L="); scanf("%d",&l); printf("\nFather id=%d\n",getpid()); x=0; makechild(); sleep(2); return 1; } int makechild() { for(q=1;q<=k;q++) { if(f=fork()) { ... (5 Replies)
Discussion started by: bb666
5 Replies
Login or Register to Ask a Question