加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

如何获取系统运行的命令状态()

发布时间:2020-12-13 19:24:20 所属栏目:Linux 来源:网络整理
导读:我在我的c代码中使用一个系统调用 #include 我当前文件夹中没有任何test12.out文件.现在输出是 sh: ./test12.out: No such file or directory0system return is 32512 这是我的shell命令失败但我怎么知道我的c代码? 编辑: 那么,我可以这样做吗? int main(

我在我的c代码中使用一个系统调用

#include 

我当前文件夹中没有任何test12.out文件.现在输出是

sh: ./test12.out: No such file or directory
0
system return is 32512

这是我的shell命令失败但我怎么知道我的c代码?

编辑:

那么,我可以这样做吗?

int main(int argc,char *argv[])
{
    int a = system("dftg");

    if(a == -1)
        printf("some error has occured in that shell command");
    else if (WEXITSTATUS(a) == 127)
        printf("That shell command is not found");
    else
        printf("system call return succesfull with  %d",WEXITSTATUS(a));
}
最佳答案
如果a == -1,则呼叫失败.否则,退出代码为WEXITSTATUS(a).

引用man 3系统:

RETURN VALUE
       The value returned is -1 on  error  (e.g.   fork(2)  failed),and  the
       return  status  of the command otherwise.  This latter return status is
       in the format specified in wait(2).  Thus,the exit code of the command
       will  be  WEXITSTATUS(status).   In case /bin/sh could not be executed,the exit status will be that of a command that does exit(127).

       If the value of command is NULL,system() returns non-zero if the shell
       is available,and zero if not.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读