我试图用一个退出号退出我的主函数,但它总是以0退出.例如,代码:
printf("command exited with error: %i\n", commandExitError);
if(commandExitError > 0)
exit(commandExitError);
if(openError > 0)
exit(openError);
printf("I shouldn't see this if there was an error");
return 0;
Run Code Online (Sandbox Code Playgroud)
有一个奇怪的行为,如果commandExitError是0,但是openError是1,那么它会以错误1退出.但是,如果commandExitError大于零,它仍然以0退出!例如,这是一些输出commandExitError > 0:
命令退出并显示错误:512
请注意,我们从未达到打印声明"我不应该看到这个...."然后,获取我的程序的退出状态,回显$?
0
我们看到我的程序仍然以0退出,尽管它应该以512清楚地退出.
据男子说
exit()函数导致正常的进程终止,并将值
status & 0377返回给父进程
八进制377是十进制255
512 & 255 = 0
Run Code Online (Sandbox Code Playgroud)