Getting the exit code of a shell script, in a C program
After you have closed a stream, you cannot perform any additional operations on it.
You should not call read
or write
or even pclose
after you called pclose
on a file object!
pclose
means you are done with the FILE *
and it will free all underlying data structures (proof).
Calling it the second time can yield anything, including 0
.
Your code should look like this:
...int r = pclose(shell);if(WEXITSTATUS(r)==3){ printf("AAA\n");}else if(WEXITSTATUS(r)==4){ printf("BBB\n");} else { printf("Unexpected exit status %d\n", WEXITSTATUS(r));}...