August 20th, 2003, 04:22 AM
Urgent - Return a value from shell script to C
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!
August 20th, 2003, 11:04 AM
Depends on what you are after.
If you want the exit status of the script, then that is returned in the status variable in wait() or waitpid().
If you want to read the output of the shell, then piping stdout to your program with popen() might work.
Or you could redirect the script's output to a tmp file which you can then open and read within your program.
I'm sure there are other ways, but this might get you started.
August 21st, 2003, 04:27 AM
Actually I am reading the output of the shell. I have used popen() finally and it works! There is a little trouble that I need to remove the newline char after reading from popen(). Anyway, I have completed the task.
Thank you, dwise1_aol!