简体   繁体   中英

Checking the return value of a C program in a bash script?

I have a bash script in which i check the exit code of a last run command by using $? variable but now I am executing a C program (from that script) which returns 0 if the program gets executed successfully. Is there any way I can catch this return value of the C program from with in my bash script?

I believe different commands like awk, sed etc are written in C. How do they use $? to store their exit codes in it? How can I make my C program to store its exit code in $??

I hope my question is clear.

There's no need to do anything - if your C program returns 0, that's what will be stored in the $? variable of the shell that executed it.

bash catches the exit code in $? automagically. Or you can just use the command in if if you only care about zero/non-zero.

The return code of a C program is the value returned by int main() function or the argument of exit() function. The system then makes it available to its parent process through the wait() system call. When the parent process is bash , this value is then made available through the $? variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM