Check the exit status in a pipe call

Guys, I have a problem :confused: 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:

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 !

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.

Had you searched the forums, you would have found this.

1 Like

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.

It couldn't help me.

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

Again:

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

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:

$ 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
$
1 Like

Problem solved.

tks !