LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   exit status (https://www.linuxquestions.org/questions/programming-9/exit-status-246007/)

naflan 10-22-2004 12:06 PM

exit status
 
I have a C application that returns a value to the shell.

Code:

exit(status);
I can get this value from the shell by using

echo $?

but, when I use it in the following way, I always get 0 even when status is not 0:

c_app | mutt -s "$?" email.address

mutt is a mail program, -s "subject line"

What is overwriting $? ?

avarus 10-22-2004 12:14 PM

Hi Naflan,

You actually have two problems here. The first is that $? is evaluated by the shell before it even thinks about running your program.

The second is that mutt is actually started before c_app finishes, such is the nature of piped execution, so mutt could never get the return value of c_app anyway.

As far as I can tell the only simple way to do what you want is to use an intermediate file for the output, then invoke mutt on the next line to read the file back, but maybe there is a more cunning solution.

TIM

naflan 10-22-2004 12:22 PM

Thanks Avarus,
I thought that I would have to do this with a script.
1. c_app > temp.txt
2. capture $? in a variable.
3. Run mutt using the variable in my subject line.
mutt -s "$variable" email@address.com < temp.txt


Quote:

The second is that mutt is actually started before c_app finishes, such is the nature of piped execution, so mutt could never get the return value of c_app anyway
I had not thought of it this way. Thanks

naflan


All times are GMT -5. The time now is 08:12 PM.