开发者

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

开发者 https://www.devze.com 2023-03-17 22:54 出处:网络
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\");

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

精彩评论

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