Forwarding return value of shell script to C/C++ [closed] Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Forwarding return value of shell script to C/C++ [closed] without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I tried to find possible solution on Google, but all I found is explanation on how to return value, but not how to accept it in some programming language, in my case C/C++? Does anybody have idea on how to do this? Some tips?


I’m focussing on Linux, but my answer probably fits for other POSIX systems.

You’ll execute your shell script from a C or C++ program with some of:

  • the usual mixture of fork(2), execve(2), waitpid(2), and then waitpid gives you the exit code using WEXITSTATUS when  WIFEXITED

  • the system(3) C standard library function. It returns what the internal waipid implementing it is returning

  • the popen(3) POSIX standard library function. Use WEXITSTATUS on the result of pclose

Both system & popen are using the fork, execve, waitpid system calls (and some others, see syscalls(2) for a list).

Read any good book on Linux system programming for details (e.g. Advanced Linux Programming by M.Mitchell et al; you’ll find copies on the Web, or APUE etc…)

We are here to answer your question about Forwarding return value of shell script to C/C++ [closed] - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji