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

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

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.

回答1:

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.



回答2:

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



回答3:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!