Return value of piped command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Return value of piped command?
# 1  
Old 03-21-2008
Return value of piped command?

Code:
grep $SEARCH_STRING /etc/passwd | cut -d":" -f 1,5

I need to check the $? value of grep in the above. If I place a test for $? after the above piped command, it returns success status of grep piped to cut.

How can I get the success status of grep alone?
# 2  
Old 03-21-2008
Different shells have different mechanisms for dealing with this situation.

Bash 3.0+ and ksh93 have the pipefail option (set -o pipefail) which changes the exit code behavior of pipelines and reports the exit code of the pipeline as the exit code of the last program to return a non-zero exit code.

Bash has the PIPESTATUS array variable which contains a list of exit status values from the processes in the most-recently-executed foreground pipeline

zsh has pipestatus.

Another way is to use coprocesses.
# 3  
Old 03-21-2008
I'm using ksh and the command set -o pipefail fails with the message
Code:
ksh: pipefail: bad option(s)

Can you please throw more light on coprocesses and the usage?
# 4  
Old 03-21-2008
Quote:
I'm using ksh and the command set -o pipefail fails with the message .....
Then you are apparantly using either ksh88 or pdksh.
# 5  
Old 03-21-2008
You to combine exit statuses logically, so that you can test more than one thing at a time.

Code:
statement1 && statement2

means execute statement1 and if its exit status is 0 (sucessful) then execute statement2

Code:
statement1 || statement2

means execute statement1 and if its exit status is not 0 then execute statement2


Regards
# 6  
Old 03-23-2008
But you can't do that in a pipeline.

Another makeshift solution would be to invoke a subshell and have it somehow smuggle out the exit status to a dedicated file descriptor. I don't think we want to go there, though.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use cut output as variable piped awk command

Hi, I would like use the output of my cut command as a variable in my following awk command. Here's what I've written. cut -f1 info.txt | awk -v i=xargs -F'' '{if($6 == $i) print $20}' summary.txt Where obviously the 'xargs' doesn't do what I want. How can I pass my cut result to my awk... (3 Replies)
Discussion started by: heyooo
3 Replies

2. Shell Programming and Scripting

Passing an argument using alias to piped command

Hi. I'm trying to do a "simple" thing. grep -rls grepped_exp path | xgs where xgs is an alias to something like: xargs gvim -o -c ":g/grepped_exp" now the problem is that I want to pass the "grepped_exp" to the piped alias. I was able to do something like what I want without the... (4 Replies)
Discussion started by: hagaysp
4 Replies

3. UNIX for Dummies Questions & Answers

Finding the modified timestamp of files from the piped output of du command

Version Info +++++++++++++++ RHEL 5.4 Since ls command lists file sizes in Bytes which can be long I use du command like below. I have run the du command for the below files as shown below. But I want pipe this output to ls command just to see the modified timestamp for these files. ... (7 Replies)
Discussion started by: kraljic
7 Replies

4. Shell Programming and Scripting

Can't Output Piped Perl In-line command to a File

Hello, I'm pretty stumped, and I don't know why I am not able to redirect the output to the 'graphme' file with the command below in Fedora 18. tcpdump -l -n -t "tcp == 18" | perl -ane '($s,$j)=split(/,/,$F,2); print "$s\n";' > graphme In case you're wondering, I was following the example... (2 Replies)
Discussion started by: ConcealedKnight
2 Replies

5. UNIX for Dummies Questions & Answers

Confused over results using "tee" in piped command

First post here, so hopefully all guidelines are followed, and thanks in advance for any replies. I'm working on a shell script(BASH) that processes a csv file and performs various tasks with the data. All is well, except I want to use 'tee' to send output from 'wc' to a file as well as pipe it... (4 Replies)
Discussion started by: jazzmusic
4 Replies

6. Shell Programming and Scripting

Unable to find exit status of piped command

Lois_Answer_Code=`sipsak -vv -s sip:192.168.1.3|grep -A 1 "reply received after"|grep SIP|awk '{print $2}'`How to find the exit status of | (12 Replies)
Discussion started by: proactiveaditya
12 Replies

7. Shell Programming and Scripting

How to execute piped command using exec or system

Hi All, I want to execute a piped command like 'ls /opt | grep xml' using array as parameters list. How can I do that? (2 Replies)
Discussion started by: bharadiaam
2 Replies

8. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

9. Shell Programming and Scripting

perl... how to tell if a piped command is still running?

I'm using the fabulous perl. I need a way to tell when a piped call to "open" has completed. Can I do this with a command like <ShellPipe> ?? Reason behind this: I'm trying to write a backup script in perl! This script will download a certain file from my web server, to my computer. Now,... (0 Replies)
Discussion started by: boytheo
0 Replies
Login or Register to Ask a Question