我有一个简单的脚本
#!/usr/bin/bash
# exit5.bash
exit 5
我在c程序中用system调用它
#include <stdlib.h>
#include <stdio.h>
int main()
{
int ret = system("./exit5.bash");
printf("%d\n", ret);
return 0;
}
我看到屏幕上打印了 1280,这与 5 << 8
相同
为什么我看不到常规 5?
最佳答案
system
的返回值是终止状态,而不是退出代码。
参见 return value section of man system
:
In the last two cases, the return value is a "wait status" that can be examined using the macros described in waitpid(2). (i.e.,
WIFEXITED()
,WEXITSTATUS()
, and so on).
WEXITSTATUS
宏返回您要查找的内容:
WEXITSTATUS(wstatus)
returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to
exit(3)
or_exit(2)
or as the argument for a return statement inmain()
. This macro should be employed only ifWIFEXITED
returned true.
关于c - 为什么 "system"的返回与被调用脚本的返回不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56298369/