c - How to get the status of command run by system() -
c - How to get the status of command run by system() -
see using 1 scheme phone call in c code
#include <sys/stat.h> #include <stdio.h> int main(int argc, char *argv[]) { int = system("./test12.out"); //here if giving wrong commad system("echo $?") printf("system homecoming %d",a); } there isnt test12.out file in current folder. output
sh: ./test12.out: no such file or directory 0 scheme homecoming 32512 here shell command got fail how can know in c code?
edit:
so can go
int main(int argc, char *argv[]) { int = system("dftg"); if(a == -1) printf("some error occured in shell command"); else if (wexitstatus(a) == 127) printf("that shell command not found"); else printf("system phone call homecoming succesfull %d",wexitstatus(a)); }
if a == -1, phone call has failed. otherwise, exit code wexitstatus(a).
to quote man 3 system:
return value value returned -1 on error (e.g. fork(2) failed), , homecoming status of command otherwise. latter homecoming status in format specified in wait(2). thus, exit code of command wexitstatus(status). in case /bin/sh not executed, exit status of command exit(127). if value of command null, system() returns non-zero if shell available, , 0 if not. c linux shell
Comments
Post a Comment