#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    2
    Rep Power
    0

    Smile Urgent - Return a value from shell script to C


    Hi,

    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!


    Florence =)
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    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.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    2
    Rep Power
    0

    Smile


    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!

IMN logo majestic logo threadwatch logo seochat tools logo