-   Programming (
-   -   return value from shell script to c code? (

khucinx 05-13-2004 02:48 PM

return value from shell script to c code?

I have a problem with returning a value from shell script to C program. I use system(cmd) to execute a sh file in a C program. How can I get a specific value in the sh and pass it back to my C program? Can it be done by setting environment variable? It seems impossible because parent and child process has it own set of environment variables. Is there any way to accomplish that?

Please help! Thanks!

paulsm4 05-13-2004 03:43 PM

No, you can't set an environment variable in a child process and expect to propagate that change back to the parent process.

And yes, you can absolutely "pass a specific value" from a shell script back to the calling C program. There are *countless* different ways. A few that come immediately to mind are:

1. Use "result = system (cmd)" to read back the return status from the shell program

2. Use "popen (cmd, "r")" instead of "system (cmd)" and then read back whatever
the shell writes to stdout ('popen()" uses pipes to let you read the shell program's
output as though it were a disk file).

3. Have your shell write some output to a data file, then have your C program
open and read the file.

Etc etc etc

It all depends on what your requirements are, and what tools you have at hand.

'Hope that helps .. PSM

All times are GMT -5. The time now is 07:12 PM.