Check the exit status in a pipe call


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check the exit status in a pipe call
# 1  
Old 10-05-2011
Check the exit status in a pipe call

Guys, I have a problem Smilie and I need some help:

I've to process many huge zip files.
I'd code an application that receive the data from a pipe, so I can simple unzip the data and send it (via pipe) to my app.

Something like that:
Code:
gzip -dc <file> | app

The problem is: How can I check if there is any problem in the gzip (exit status) from my app?

Thanks !

Last edited by Franklin52; 10-06-2011 at 03:18 AM.. Reason: Please use code tags, thank you
# 2  
Old 10-05-2011
Depends on your shell. BASH has an array PIPESTATUS containing the return statuses of the last pipe chain you ran. I'm not sure how ksh does it. If you're in a basic bourne shell, that'd be tricky to do at all.
# 3  
Old 10-05-2011
Had you searched the forums, you would have found this.
This User Gave Thanks to vgersh99 For This Post:
# 4  
Old 10-05-2011
I forgot to say that I'm using bash.

The problem is that my app needs to know that there is any problem. If the app finds any problem it must do a rollback.

Did I make myself clear ?

Tks
# 5  
Old 10-05-2011
Quote:
Originally Posted by Rkolbe
I forgot to say that I'm using bash.

The problem is that my app needs to know that there is any problem. If the app finds any problem it must do a rollback.

Did I make myself clear ?

Tks
The quoted links should get you started.
# 6  
Old 10-05-2011
Quote:
Originally Posted by vgersh99
The quoted links should get you started.
It couldn't help me.

As I said my app (an C app) have to be informed if the GZIP got any problem.

Again:
Code:
gzip -dc my_file.gz | my_app

If there is any problem in the gzip uncompression process, my_app must be informed to 'rollback' any previously process.

Thanks

Last edited by Franklin52; 10-06-2011 at 03:19 AM.. Reason: Please use code tags, thank you
# 7  
Old 10-05-2011
Quote:
Originally Posted by Rkolbe
As I said my app (an C app) have to be informed if the GZIP got any problem.
If your app doesn't have any idea where the info is supposed to end, that's a problem. if gzip dies instantly that's one thing, no data read, but if it dies halfway through? It doesn't get gzip's exit status and doesn't know gzip wasn't supposed to end there.

Your code could run its own gunzip instead, and would be able to get its exit status that way.

---------- Post updated at 02:04 PM ---------- Previous update was at 02:00 PM ----------

Something like:

Code:
$ cat popen.c
#include <stdio.h>
#include <sys/wait.h> // for wexitstatus
#include <sys/types.h>

int main(void)
{
        int status;
        char buf[512];
        FILE *fp=popen("gunzip", "r"); // Decompress, reading from stdin

        while(fgets(buf, 512, fp)) // Read until EOF
                printf("%s", buf);

        if(WEXITSTATUS(status=pclose(fp)))
                printf("gunzip failed with status %d, do rollback\n", WEXITSTATUS(status));
}
$ gcc popen.c
$ echo asdf | gzip | ./a.out
asdf
$ echo asdf | ./a.out

gzip: stdin: not in gzip format
gunzip failed with status 1, do rollback
$

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check file if not found send mail if exit call second script

I need to check my script and change to working mode. currently it was not sending the mail and exit without calling the second script. I need to check the file is present ="/home/Rvtools/test.csv" if this file not found after the time retry send mail file not found If the file exit run the... (2 Replies)
Discussion started by: ranjancom2000
2 Replies

2. Shell Programming and Scripting

Check/get the exit status of a remote command executed on remote host through script

Geeks, Could you please help me out in my script and identify the missing piece. I need to check/get the exit status of a remote command executed on remote host through script and send out an email when process/processes is/are not running on any/all server(s). Here's the complete... (5 Replies)
Discussion started by: lovesaikrishna
5 Replies

3. Shell Programming and Scripting

How to check exit status of unset variables

Hi All, Is there any way to check exit status of unset variables? In the following code PathX is not set and the script terminates without checking exit status. #!/bin/bash Path="/tmp/log" cd ${PathX:?} if ;then echo "Exit Status : non zero" else echo "Exit Status :... (2 Replies)
Discussion started by: sussus2326
2 Replies

4. Shell Programming and Scripting

Check for exit status

Hi I have following code I want If whole code executes successfully then return true If found any error then print the error I tried if ; then But this checks only for the just upper line execution #!/bin/bash PATH1=/var/log/mysql PATH2=/home/ankur/log FILE1=mysql-bin.index... (4 Replies)
Discussion started by: kaushik02018
4 Replies

5. Shell Programming and Scripting

check exit status of bg scripts

HI All, I am running one shell script, in that script i am calling 4 scripts in the background. abc.ksh & efg.ksh & xky.ksh & mno.ksh & please let me know, how could i find the success and failure of each script. i Cannot use $?, because i want to run all the scripts in parellel. ... (2 Replies)
Discussion started by: javeed7
2 Replies

6. Shell Programming and Scripting

How to get the exit status

Hi all, I'm running a program which return 1 upon success. But when encounters problem shell return 's '1' . How to differentiate between them the shell return value and script return value. Ex. function fn return '1' if executed successfully and '0' if failed. But when if shell encounters... (1 Reply)
Discussion started by: yhacks
1 Replies

7. Shell Programming and Scripting

check exit status - Expect Script

from my main script, i am calling an expect script. there are a lot of conditions in the Expect script and it can have any exit value based on success or failure of the Expect Script. how can i check the exit status of Expect scritp in the main script. (1 Reply)
Discussion started by: iamcool
1 Replies

8. UNIX for Dummies Questions & Answers

exit status of command in a pipe line

Hi, I am trying to test the exit status of the cleartool lsvtree statement below, but it doesn't seem to be working due to the tail pipe, which it is testing instead. Is there a way around this without adding a tonne of new code? cleartool lsvtree $testlocation/$exe_name | tail -15 ... (10 Replies)
Discussion started by: topcat8
10 Replies

9. UNIX for Dummies Questions & Answers

how to check exit status in awk script

Hi, I have a main program which have below lines - awk -f test.awk inputFileName - I wonder how to check status return from awk script. content of awk script: test.awk --- if ( pass validation ) { exit 1 } else { (1 Reply)
Discussion started by: epall
1 Replies

10. Shell Programming and Scripting

check the status and send an email with status

Hi, We have a text file which has the following data. ISA~00~ ~00~ ~ZZ~VISTN ~ZZ~U1CAD ~051227~183 7~U~00200~000011258~0~P~< GS~FA~EE05J~U1CAD~051227~1831~000011258~X~002002 ST~997~0001 AK1~SH~247 AK2~856~2470001 AK5~A AK2~856~2470002 AK5~A... (3 Replies)
Discussion started by: isingh786
3 Replies
Login or Register to Ask a Question