Linux 如何获取 system() 运行的命令的状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8941691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the status of command run by system()
提问by Jeegar Patel
I am using one system call in my c code
我在我的 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
system("echo $?")
printf("system return is %d",a);
}
there isn't any test12.out file in my current folder. Now output is
我的当前文件夹中没有任何 test12.out 文件。现在输出是
sh: ./test12.out: No such file or directory
0
system return is 32512
Here is my shell command failed but how can I know that in my c code?
这是我的 shell 命令失败了,但我怎么知道在我的 c 代码中呢?
Edit:
编辑:
So, can I do this
那么,我可以这样做吗
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));
}
采纳答案by NPE
If a == -1
, the call has failed. Otherwise, the exit code is WEXITSTATUS(a)
.
如果a == -1
,则调用失败。否则,退出代码为WEXITSTATUS(a)
。
To quote man 3 system
:
引用man 3 system
:
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.
回答by James McLaughlin
Try using WEXITSTATUS
:
尝试使用WEXITSTATUS
:
int a = WEXITSTATUS(system("./test12.out"));
回答by Victor Sorokin
Check that a is not 0
. Your 2nd line shows 0
because it's executed in different shell with no prior history, so that brand new shell reports "All is ok" to you.
检查 a 不是0
。您的第 2 行显示是0
因为它是在没有先前历史记录的不同 shell 中执行的,因此全新的 shell 会向您报告“一切正常”。
回答by Dimitri
When you read the man in the opengroup website, it says :
当您阅读 opengroup 网站上的那个人时,它说:
If command is a null pointer, system() shall return non-zero to indicate that a command processor is available, or zero if none is available. [CX] The system() function shall always return non-zero when command is NULL.
[CX] If command is not a null pointer, system() shall return the termination status of the command language interpreter in the format specified by waitpid(). The termination status shall be as defined for the sh utility; otherwise, the termination status is unspecified. If some error prevents the command language interpreter from executing after the child process is created, the return value from system() shall be as if the command language interpreter had terminated using exit(127) or _exit(127). If a child process cannot be created, or if the termination status for the command language interpreter cannot be obtained, system() shall return -1 and set errno to indicate the error.
如果 command 是空指针,则 system() 应返回非零以指示命令处理器可用,如果没有可用,则返回零。[CX] 当 command 为 NULL 时,system() 函数应始终返回非零值。
[CX] 如果 command 不是空指针,则 system() 应以 waitpid() 指定的格式返回命令语言解释器的终止状态。终止状态应与 sh 实用程序的定义相同;否则,终止状态是未指定的。如果在创建子进程后某些错误阻止命令语言解释器执行,则 system() 的返回值应如同命令语言解释器已使用 exit(127) 或 _exit(127) 终止。如果无法创建子进程,或者无法获得命令语言解释器的终止状态,则 system() 应返回 -1 并设置 errno 以指示错误。
回答by Venkatesh Parthasarathy
Use
用
system("your command; echo $?");
echo $?
-- will provide you the exit status of command.
echo $?
-- 将为您提供命令的退出状态。
(Output of command can be avoided using redirection to /dev/null if you need only exit status)
(如果您只需要退出状态,可以使用重定向到 /dev/null 来避免命令输出)