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

如何获取系统运行的命令状态(),第1张

概述我在我的c代码中使用一个系统调用#include <sys/stat.h> #include <stdio.h> int main(int argc, char *argv[]) { int a = system('./test12.out'); //here if i give any wrong command

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

#include 

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

sh: ./test12.out: No such file or directory0system 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.
总结

以上是内存溢出为你收集整理的如何获取系统运行的命令状态()全部内容,希望文章能够帮你解决如何获取系统运行的命令状态()所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1047922.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)