LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   link c++ and bash? (https://www.linuxquestions.org/questions/programming-9/link-c-and-bash-406366/)

Ha1f 01-21-2006 09:19 PM

link c++ and bash?
 
Hi all,

I'd like to know if this is possible. I'm writing a C++ program that I want to run bash scripts. The thing is, I'd like to be able to take the result of the script and use it as a value in my C++ program. For instance, if the script failed (either because of an error, or because it acheived a value within itself that would return a failure) I'd like the C++ program to be able to take that value and use it. Is this possible? If so, how?

Thanks

carl.waldbieser 01-21-2006 09:54 PM

Quote:

Originally Posted by Ha1f
Hi all,

I'd like to know if this is possible. I'm writing a C++ program that I want to run bash scripts. The thing is, I'd like to be able to take the result of the script and use it as a value in my C++ program. For instance, if the script failed (either because of an error, or because it acheived a value within itself that would return a failure) I'd like the C++ program to be able to take that value and use it. Is this possible? If so, how?

Thanks

The easiest way would be to have your bash script emit the information you want from it to standard output or standard error. Then you could just use the normal C++ facilities to read from those streams.

There are other techniques for IPC (inter-process communication), but in *NIX environments, the standard streams are some of the simplest to work with.

paulsm4 01-21-2006 10:50 PM

And of course there's always "popen()": it'll read stdout from your script; it'll also read your script's "exit" status: whichever (or both) you prefer.

"man 3 popen", if you're interested.

Ha1f 01-22-2006 07:11 AM

wow--thats awesome.

thanks so much guys!!


All times are GMT -5. The time now is 10:17 PM.