开发者

how to invoke another program and get return value by $?

I am using popen() to invoke another program and want to get its return value by $?

ex:

FILE* fd = popen("/usr/local/my_check > /dev/null ; echo $?","r");
int read_开发者_Go百科num = fread(buffer, sizeof(char), BUFFER_SIZE, fp);
printf("%s\n", buffer);
pclose(fd);

but I always get zero in prinf function.

any other way to get return value by $? in c program?

thanks!

here is the correct way to get return code of program:

int ret = pclose(fd);
if(WIFEXITED(ret))
  printf("%d\n", WEXITSTATUS(ret));


You can get the exit code with

int rc = pclose(fd)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜