Setting environment variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sami Viitanen

    Setting environment variables

    Hello,

    Is there any other way to set variables than os.putenv().

    Putenv doesn't actually put any values to actual system variables..

    I'm trying to set CVSEDITOR variable automatically from script
    so that user wouldn't have to set that him/herself. CVS can't use variable
    set with putenv().


    Thanks in advance.


  • Erik Max Francis

    #2
    Re: Setting environment variables

    Sami Viitanen wrote:
    [color=blue]
    > Is there any other way to set variables than os.putenv().
    >
    > Putenv doesn't actually put any values to actual system variables..
    >
    > I'm trying to set CVSEDITOR variable automatically from script
    > so that user wouldn't have to set that him/herself. CVS can't use
    > variable
    > set with putenv().[/color]

    Yep, and this is a feature, at least in Unix operating systems.
    Subshells cannot directly affect the operating environment of parent
    shells, and shouldn't try. (One can do such things indirectly, but they
    require the cooperation of the parent shell, such as sourcing the output
    of a program, which is not an uncommon approach to the problem.) I
    don't know if there's any way for Windows applications to affect parent
    environments, but I doubt it.

    --
    Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ Perfect situations must go wrong
    \__/ Florence, _Chess_

    Comment

    • Peter Hansen

      #3
      Re: Setting environment variables

      Erik Max Francis wrote:[color=blue]
      >
      > Sami Viitanen wrote:
      >[color=green]
      > > Is there any other way to set variables than os.putenv().
      > >
      > > Putenv doesn't actually put any values to actual system variables..
      > >
      > > I'm trying to set CVSEDITOR variable automatically from script
      > > so that user wouldn't have to set that him/herself. CVS can't use
      > > variable
      > > set with putenv().[/color]
      >
      > Yep, and this is a feature, at least in Unix operating systems.
      > Subshells cannot directly affect the operating environment of parent
      > shells, and shouldn't try. (One can do such things indirectly, but they
      > require the cooperation of the parent shell, such as sourcing the output
      > of a program, which is not an uncommon approach to the problem.) I
      > don't know if there's any way for Windows applications to affect parent
      > environments, but I doubt it.[/color]

      Short of bizarre hacks that are generally unacceptable, no there's no
      way to do it even under Windows.

      Under both operating systems, the best approach is to contrive a way
      for another script to be executed *after* the application runs. That
      other script either accesses data that is generated by the application,
      or is actually itself generated by the application as needed, and because
      it's a script (e.g. .BAT or .CMD for Windows, .sh etc. for Unix) it
      is able to change the shell. (And I know you already knew this, EMF,
      I was of course responding for others. :-)

      -Peter

      Comment

      • David C. Fox

        #4
        Re: Setting environment variables

        Erik Max Francis wrote:[color=blue]
        > Sami Viitanen wrote:
        >
        >[color=green]
        >>Is there any other way to set variables than os.putenv().
        >>
        >>Putenv doesn't actually put any values to actual system variables..
        >>
        >>I'm trying to set CVSEDITOR variable automatically from script
        >>so that user wouldn't have to set that him/herself. CVS can't use
        >>variable
        >>set with putenv().[/color]
        >
        >
        > Yep, and this is a feature, at least in Unix operating systems.
        > Subshells cannot directly affect the operating environment of parent
        > shells, and shouldn't try. (One can do such things indirectly, but they
        > require the cooperation of the parent shell, such as sourcing the output
        > of a program, which is not an uncommon approach to the problem.) I
        > don't know if there's any way for Windows applications to affect parent
        > environments, but I doubt it.
        >[/color]

        In [t]csh:

        alias cvsscript "setenv CVSEDITOR `python myscript.py`"
        cvsscript

        or miscellaneous variants in other shells. Of course, you can't use
        cvsscript from within another subshell, or it will only set the variable
        in that subshell.

        David


        Comment

        • Donald 'Paddy' McCarthy

          #5
          Re: Setting environment variables

          Sami Viitanen wrote:[color=blue]
          > Hello,
          >
          > Is there any other way to set variables than os.putenv().
          >
          > Putenv doesn't actually put any values to actual system variables..
          >
          > I'm trying to set CVSEDITOR variable automatically from script
          > so that user wouldn't have to set that him/herself. CVS can't use variable
          > set with putenv().[/color]

          You could do the equivalent of:
          Paddy@maximilli an ~ : echo $SHELL
          /bin/bash
          Paddy@maximilli an ~ : unset foo
          Paddy@maximilli an ~ : eval `python -c 'print "foo=Hello;expo rt foo"'`
          Paddy@maximilli an ~ : echo $foo
          Hello
          Paddy@maximilli an ~ :

          I use the following for setting up quite complex environments at work:
          The official web page for the Modules software package. The Modules package provides for the dynamic modification of a user's environment via modulefiles.


          Pad.

          Comment

          Working...