Adding ENV variables in linux

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coolvjay
    New Member
    • Feb 2007
    • 4

    Adding ENV variables in linux

    I was writing a perl program where I have to add one environment variable.
    like RCS_ROOT.
    I tried to use system( " setenv RCS_ROOR ****");
    i also tried system(" export RCS_ROOT=****** **");
    But nothing is working .. when i run it tell no file called setenv.
    Please let me know how to set userdefined env. using perl.
    Basic idea is set some user defined paths with setup script
    and later use those paths in diff. scripts to locate files.

    please help

    regards
    vijay
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    You may want to have a look at the %ENV hash. In this hash Perl maintains the environment variables.

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      I'm pretty sure he got the answer on another forum.

      Comment

      • coolvjay
        New Member
        • Feb 2007
        • 4

        #4
        No I have not found any answer!!
        well I tried to manipulate %ENV but %ENV contains list of only existing variables
        but I want to add new variable which does not exist...

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          maybe:

          $ENV{RCS_ROOT} = "your value here";

          only works for the duration of the script though. If you need it to be persistent write it to a file and read the file each time the script is envoked. Maybe somone else knows a better way.

          Comment

          • coolvjay
            New Member
            • Feb 2007
            • 4

            #6
            thanks kevin this will solve the problem but still my doubt is why system does not execute the setenv?
            I tried to write shell script and then execute the script even with that I am not able to see the variable added or set !!!!!
            well I wil try to do by ur method.....


            thanks

            Originally posted by KevinADC
            maybe:

            $ENV{RCS_ROOT} = "your value here";

            only works for the duration of the script though. If you need it to be persistent write it to a file and read the file each time the script is envoked. Maybe somone else knows a better way.

            Comment

            • miller
              Recognized Expert Top Contributor
              • Oct 2006
              • 1086

              #7
              First off, it sounds like you should simply create a file to save these settings in. That is the best way to ensure that the settings are persistant. Then you simply have any of your subsequent scripts import the data. You could even still use the $ENV variables method.

              For example, say that this was your settings file. ".settings"
              Code:
              $ENV{RCS_ROOR} = "your value here";
              Then all scripts that needed this value could simply import it by doing an eval or require. Then simply access the data using the environment variable.

              Code:
              require ".settings";
              
              print "$ENV{RCS_ROOR}";
              If you do not need persistance, then making a simple assignment like in Kevin's example would work fine.

              Concerning why setenv and export do not work. This is something that I've attempted before and eventually abandoned as a bad approach. Basically, my understanding is that setenv and export are something that is specific to the shell environment thatyou are using, whether it's tsh or bash, etc. You could try to do a system("bash export foo='bar'") call, but that would only last for that one statement. The only way to effect all subsequent shell environments is to add the export call to the .bashrc or .tcshrc start routine. And even then, that's not going to effect any shell environments that are already spawned.

              Anyway, the moral of the story is to not rely on the environment that you're running perl under to set configuration data for other perl scripts. Instead create a configuration directory or file that all the scripts can share. Or if you simply need it for one session, use the method that Kevin suggests.

              - Miller

              Comment

              • coolvjay
                New Member
                • Feb 2007
                • 4

                #8
                Thank you Miller for help
                I dropped Idea of setting environment variables with perl script because I dont want setup script to be dependent on any other file or any other scripts to be dependent on other files. ( that was the reason I wanted to set env. variables )
                Now for setup I am using shell scripting.
                Like we source .bash or .cshrc when we login we can source this setup file everytime we login. and everything is working fine with this setup.


                thanks again
                vijay

                Comment

                • miller
                  Recognized Expert Top Contributor
                  • Oct 2006
                  • 1086

                  #9
                  That can work, and is pretty much the only way to do it using ENV variables. I would simply like to point out that you are still dependent on an external file, be it somewhat transparently.

                  Anyway, glad you found a solution that you're happy with.

                  - Miller

                  Comment

                  Working...