简体   繁体   中英

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_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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM