Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

bash

shell

popen

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));
like image 352
SILENCE Avatar asked May 30 '26 00:05

SILENCE


1 Answers

You can get the exit code with

int rc = pclose(fd)
like image 143
Aaron Digulla Avatar answered May 31 '26 12:05

Aaron Digulla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!