Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: setting enviroment variables for use in a script from a 'C' program

  1. #1
    Join Date
    Mar 2006
    Location
    Devon UK
    Beans
    419

    Question setting enviroment variables for use in a script from a 'C' program

    Hope someone can help with this, I want to be able to set an environment variable from an external c program that is readable by the calling script, I can read the variables using getenv() and can alter them using setenv() but once the program terminates the changes are lost.
    In /dev/null no one can hear the kernel panic!
    Don't EVER use sudo rm -rf / . if you don't understand a command check it out first!
    BACK UP YOUR DATA OR YOU WILL LOSE IT!!
    Supergrub - The best thing since sliced bread www.supergrubdisk.org

  2. #2
    Join Date
    Jul 2007
    Beans
    305
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: setting enviroment variables for use in a script from a 'C' program

    That's not possible. The program gets a pointer to a copy of the environment variables.
    iplist

    "Specialization is for Insects", R. Heinlein

  3. #3
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,248
    Distro
    Kubuntu

    Re: setting enviroment variables for use in a script from a 'C' program

    You could always have the C program launch the script using one of the exec family of functions; perhaps the execle() could be used.

    If launching the script from the C program is not an option, then as uljanow pointed out, you would be up the creek without a paddle. The C program, once launched (say from a shell), runs in an environment that is separate from the one that launched it. It merely has a copy of the environment variables when it is launched.

    The C program can set environment variables (using setenv()) within its own environment, but this has no effect on other environment(s).

  4. #4
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: setting enviroment variables for use in a script from a 'C' program

    I don't think starting a shell script from C will help because the script again will run in its own enviroment

  5. #5
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,248
    Distro
    Kubuntu

    Re: setting enviroment variables for use in a script from a 'C' program

    Quote Originally Posted by MadCow108 View Post
    I don't think starting a shell script from C will help because the script again will run in its own enviroment
    I believe it is possible to configure the environment in which the script runs. I do not have any experience doing this, but I think execle() allows for this.

  6. #6
    Join Date
    Aug 2007
    Beans
    949

    Re: setting enviroment variables for use in a script from a 'C' program

    When you start a sub-process, that process inherits the environmental variables of the one that launched it.

    So yes, it would help, assuming you set all the variables you wanted to, then launched the sub-processes (by forking and then exec'ing, or just straight exec'ing).

    Look at execve + buddies for replacing process images, or system() for forking then running processes in the background.

  7. #7
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: setting enviroment variables for use in a script from a 'C' program

    as far as I understand execle only allows to change the enviroment variables of the new process image but does not change the original environment
    but I never used it myself so I may have misinterpreted.

  8. #8
    Join Date
    Aug 2007
    Beans
    949

    Re: setting enviroment variables for use in a script from a 'C' program

    Yes, you can't change the original environment variables using this method. But the question is, why would you want to?

  9. #9
    Join Date
    Mar 2006
    Location
    Devon UK
    Beans
    419

    Re: setting enviroment variables for use in a script from a 'C' program

    Well I thought I was on to a loser with this one as I have tried various things and googled all sorts of combinations and had no luck, thanks for the help anyway!
    In /dev/null no one can hear the kernel panic!
    Don't EVER use sudo rm -rf / . if you don't understand a command check it out first!
    BACK UP YOUR DATA OR YOU WILL LOSE IT!!
    Supergrub - The best thing since sliced bread www.supergrubdisk.org

  10. #10
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: setting enviroment variables for use in a script from a 'C' program

    if you call the c program with a script, why not just set the enviroment variables in the script?

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •