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?
Answer
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 usingWEXITSTATUS
whenWIFEXITED
-
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 ofpclose
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…)