Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Environment Variable Setting

by austinj (Acolyte)
on Apr 23, 2012 at 16:14 UTC ( [id://966632]=perlquestion: print w/replies, xml ) Need Help??

austinj has asked for the wisdom of the Perl Monks concerning the following question:

My ultimate goal would be to write a script in perl that accomplishes the same thing the following csh code does. I tried to simply source the csh code (located in the file sge_sigma.csh) but have since learned that this creates a child process and as such environment variables are not passed upstream. SO - I have been unsucessful in converting the following code from csh to perl.
#! /bin/csh setenv SGE_ROOT /appl/lsf/grid_bins set ARCH = `$SGE_ROOT/util/arch` set DEFAULTMANPATH = `$SGE_ROOT/util/arch -m` set MANTYPE = `$SGE_ROOT/util/arch -mt` setenv SGE_CELL default unsetenv SGE_QMASTER_PORT unsetenv SGE_EXECD_PORT # library path setting required only for architectures where RUNPATH i +s not supported if ( $?MANPATH == 1 ) then setenv MANPATH $SGE_ROOT/${MANTYPE}:$MANPATH else setenv MANPATH $SGE_ROOT/${MANTYPE}:$DEFAULTMANPATH endif set path = ( $SGE_ROOT/bin/$ARCH $path ) switch ($ARCH) case "sol*": case "lx*": breaksw case "*": set shlib_path_name = `$SGE_ROOT/util/arch -lib` if ( `eval echo '$?'$shlib_path_name` ) then set old_value = `eval echo '$'$shlib_path_name` setenv $shlib_path_name "$SGE_ROOT/lib/$ARCH":"$old_value" else setenv $shlib_path_name $SGE_ROOT/lib/$ARCH endif unset shlib_path_name endsw unset ARCH DEFAULTMANPATH MANTYPE old_value
so far I have
#!/usr/local/bin/perl -w use strict; use Switch; $ENV{SGE_ROOT} = "/appl/lsf/grid_bins"; my $ARCH = $ENV{SGE_ROOT} . "/util/arch"; my $DEFAULTMANPATH = $ENV{SGE_ROOT} . "/util/arch -m"; my $MANTYPE = $ENV{SGE_ROOT} . "/util/arch -mt"; $ENV{SGE_CELL} = "default"; delete($ENV{SGE_QMASTER_PORT}); delete($ENV{SGE_EXECD_PORT}); # NEED HELP HERE
if there is a cleaner way to somehow "source" the csh file or any other suggestions I'm open to whatever. Thanks in advance for your help, Austin

Replies are listed 'Best First'.
Re: Environment Variable Setting
by chrestomanci (Priest) on Apr 23, 2012 at 16:33 UTC

    On unix, child processes inherit the environment from their parent, but any changes they make are not propagated back to the parent when the child quits.

    Your csh script works because it runs in the same process as your shell, so changes made in it remain after the script exits.

    The usual way to create a perl script that modifies the environment is to have it emit a shell script on stdout, and then eval the script's output. eg:

    eval `perl set_my_env.pl`

    The perl script just prints a series of setenv NAME value lines.

Re: Environment Variable Setting
by Corion (Patriarch) on Apr 23, 2012 at 16:30 UTC
Re: Environment Variable Setting
by aaron_baugher (Curate) on Apr 23, 2012 at 17:43 UTC

    When you source a file in the shell, that actually does what you want -- it doesn't create a sub-shell, but interprets the file in the current shell, allowing it to change the environment. That's unlike running the script as an executable, which does fire up a new shell process, which is unable to change the environment of its parent. So source sge_sigma.csh should do what you want.

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.

      this is only true within csh correct? I can't simply

      source sge_sigma.csh;

      Does perl have a "source" command?

      system("source sge_sigma.csh");

      doesn't work

      Thanks!

        If you're in a csh shell (or most other shells), then yes, entering source sge_sigma.csh at the command line will cause the contents of that file to be interpreted and take effect in the current shell.

        bannor:~/work/perl/monks$ csh bannor:~/work/perl/monks> cat test.csh setenv MYTEST "This is a test." bannor:~/work/perl/monks> echo $MYTEST MYTEST: Undefined variable. bannor:~/work/perl/monks> source test.csh bannor:~/work/perl/monks> echo $MYTEST This is a test.

        You can't do this from within a Perl script, because it's a sub-process of the shell, so it can't change its parent's environment. To put it another way, you can change environment variables within a Perl script by use of the %ENV hash, and those will be inherited by any child processes that your Perl script spawns, but they can't go "upstream" to the parent process.

        Aaron B.
        My Woefully Neglected Blog, where I occasionally mention Perl.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://966632]
Approved by kennethk
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-03-29 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found