Checking return value of commands in pipe


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Checking return value of commands in pipe
# 1  
Old 04-23-2007
Checking return value of commands in pipe

Hi,

I am wondering how I can check the return value of all commands in a pipe such as

gzip -dc file.gz | sort -u > output.txt

If I run this sequence in bash and check $?, I get the return status from sort. But I want to know if the initial gzip failed.

Similarly for longer pipe chains, I want to know if any of the components returned an error status.

I don't mind if you give a solution for another shell such as tcsh. Any solution is great!

Thanks!
# 2  
Old 04-23-2007
Code:
gzip -dc file.gz && sort -u > output.txt
echo $?

&& Only if gzip command is successful sort command is executed.
# 3  
Old 04-23-2007
But I want the output of gzip to go into sort..
# 4  
Old 04-23-2007
See if PIPESTATUS helps. From man sh
Code:
       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).

# 5  
Old 04-23-2007
Thankyou vino! That looks perfect.

Thankyou anbu also for your suggestion!
# 6  
Old 08-28-2008
But how does one do it in a shell that doesn't have PIPESTATUS? I know that autoconf (tool to install source on almost Unix that has lots of shell-independent logic in it) plays tricks with redirection and subshells to capture the return values from inner pipe commands. I just never took the time to reverse engineer it in my head to understand it.

Anyone?
# 7  
Old 06-02-2009
PIPESTATUS and autoconfigure

At the risk of sounding like a hermit (talking to myself), I recently examined some of the configure scripts and saw several patterns:
  1. There aren't many pipe commands after all, mostly simple sed calls
  2. The tricks played with redirection are for logging and/or extracting return values from simple commands used inside of subshells

So the PIPESTATUS appears to be unique in the ability to extract return values from simple pipe commands.

Quote:
Originally Posted by qneill
But how does one do it in a shell that doesn't have PIPESTATUS? I know that autoconf (tool to install source on almost Unix that has lots of shell-independent logic in it) plays tricks with redirection and subshells to capture the return values from inner pipe commands....
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

How to run commands with pipe from text file?

Hello, I have standard loop while read -r info; do command $info done < info in info text file I have multiple commands each on line that I want to execute. When I used them in console they worked, but not with this loop. This is one of the commands in info file: grep... (4 Replies)
Discussion started by: adamlevine
4 Replies

2. Shell Programming and Scripting

Pipe or combine output of three awk commands

What is the correct syntax to pipe or run three awk commands? Basically, using the output of the first awk as input in the second. Then using the output of the second awk in the third. Thank you :). awk 'FNR==NR {E; next }$3 in E {print $3, $5}' panel_genes.txt RefSeqGene.txt > update.txt |... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Ksh: run commands from pipe

I create commands within a pipe and finally want them to be executed instead of being displayed on the screen. What is the last stage in this pipe? I found by guessing that "ksh" is working, but is this the best to use here? It boils down to this: echo "print Hello World!"| kshWhat is the... (15 Replies)
Discussion started by: Cochise
15 Replies

4. AIX

Return code 1 when echo to pipe

Hello, Our AIX box has recently been upgraded to TL12 (oslevel -s = 5300-12-04-1119). Now one of our ksh scripts is returning 1 when writing to a pipe, the command to write to the pipe is: echo "A" "B" "C" >> /usr/Pipe.Pipe Anyone have any ideas? Thanks (2 Replies)
Discussion started by: dendright
2 Replies

5. UNIX for Dummies Questions & Answers

Why do these 2 find commands return different results?

Hi, I am using the korn shell on Solaris box. Why does the following 2 commands return different results? This command returns no results (I already used this command to create a list of files which I moved to an archive directory) find ????10??_*.dat -type f -mtime +91 However this... (15 Replies)
Discussion started by: stumpy1
15 Replies

6. UNIX for Dummies Questions & Answers

return code capturing for all commands connected by "|" ...

Hi, While I am using "|" to join multiple commands, I am not getting the return code when there is error in one of the commnads. Eg: b=`find /path/a*.out | xargs basename` if ; then echo "Error" fi if there is error while finding the file or getting the basename, the $? is... (6 Replies)
Discussion started by: new_learner
6 Replies

7. Shell Programming and Scripting

checking for return status between multiple commands

i have to run set of commands command1 command2 command3 command4 Now Whenever any of these command fails i should quit while capturing error message. Is there a better way then checking for $? after each command. (1 Reply)
Discussion started by: vickylife
1 Replies

8. Shell Programming and Scripting

checking the return code

hi i have a file, i am reading line by line and checking a line contains a string , `grep "Change state" $LINE` if then echo "The line contains---" else echo "The line does not contains---" i need to check the return code , but i am getting an error ... (4 Replies)
Discussion started by: Satyak
4 Replies

9. Shell Programming and Scripting

Checking Return Codes of Background Processes

I'm trying to do the following: 1) Run a bunch of jobs in the background 2) Determine if any one of them returns with a non-zero exit status Here's what I've come up with so far: ########################################## #!/bin/ksh while } -lt 1024 ] do SLEEP_TIME=`expr 1024 -... (2 Replies)
Discussion started by: bergerj3
2 Replies

10. UNIX for Dummies Questions & Answers

return codes from rsh commands...

I have a Korn shell script that executes a number of commands on a remote server. Is it possible to feed in the last exit code of the rsh commands (i.e. something like $?) to a variable within the local shell script? I tried the following: returncode=$(rsh spns31 ".... (1 Reply)
Discussion started by: bbouch
1 Replies
Login or Register to Ask a Question