Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

closed as off-topic by muru, chaos, Archemar, cuonglm, G-Man Sep 23 '15 at 8:28

  • This question does not appear to be about Unix or Linux within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Define what is the return value of a shell script? Are you thinking of its exit code? Details could be operating system specific.... Also, explain what "forwarding" mean to you... – Basile Starynkevitch Sep 23 '15 at 7:50
    
Example: Shell script for initializing connection with server, if it is okay return 1 if it is not return 0. @BasileStarynkevitch – Junior Sep 23 '15 at 7:52
    
This is called the exit code. Conventionally, 0 is EXIT_SUCCESS so is for success. – Basile Starynkevitch Sep 23 '15 at 7:53
up vote 2 down vote accepted

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

share|improve this answer
    
Recommend me some book. Thanks for answer. – Junior Sep 23 '15 at 8:02
    

Not the answer you're looking for? Browse other questions tagged or ask your own question.