退出代码,返回回显与程序的退出代码不同。为什么?

时间:2019-05-26 10:33:21

标签: bash unix exec solaris exit-code

我编写了一个使用进程返回错误代码的程序。 该程序的工作结果(如果在程序的入口提供了false命令)为255。 但是命令 假;回声$? 返回1 为什么会发生? Solaris,Unix

我在源代码中找到了文件false.c,它返回255(不确定这是否是正确的命令) https://github.com/illumos/illumos-gate/blob/master/usr/src/cmd/false/false.c

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <wait.h>

main(int argc, char *argv[])
{
        int status;
        pid_t pid = fork();
        if (pid == -1){
                perror("fork error");
                exit(-1);
        }
        if (pid == 0) {
                execvp(argv[1], &argv[1]);
                perror(argv[1]);
                exit(-5);
        }
        if( wait(&status) == -1){
                perror("wait");
                exit(-1);
        }
        if(WIFEXITED(status))
                printf("exit status: %d\n",WEXITSTATUS(status));

        exit(0);
}

1 个答案:

答案 0 :(得分:0)

UNIX(Linux,Solaris,BSD等)退出代码只能是0-255,其中0是好的,非零是错误。退出代码未签名,因此-1可能会转换为另一个(非零)值,超过255的值也是如此。