Loading the shell environment?

Discussion in 'Perl Misc' started by T, Oct 10, 2008.

  1. T

    T Guest

    Greetings:

    We use Modules ( I hate that they chose that name) to load our
    shell environment here. So I can do:

    mod load myproject

    "mod" is an alias to:

    /cae/Modules/${MODULE_VERSION}/bin/modulecmd bash load hagrid
    T, Oct 10, 2008
    #1
    1. Advertising

  2. T

    T Guest

    On Oct 10, 11:22 am, T <> wrote:
    > Greetings:
    >
    >     We use Modules ( I hate that they chose that name) to load our
    > shell environment here. So I can do:
    >
    >     mod load myproject
    >
    >    "mod" is an alias to:
    >

    Sorry about that...
    geting back

    /cae/Modules/${MODULE_VERSION}/bin/modulecmd bash load myproject

    in a bash shell script I can do:

    eval `/cae/Modules/${MODULE_VERSION}/bin/modulecmd bash load
    myproject`

    and this works, I tried the equivlant in perl:

    eval `/cae/Modules/$ENV{'MODULE_VERSION'}/bin/modulecmd tcsh load
    myproject`

    along with several other variants without any luck. I was hoping to
    avoid writing
    a shell wrapper around my Perl script. Does anyone know how to load
    the shell environment
    in a Perl script? This script will run in crontab file that's why I
    need to load the env.

    Thanks in Advanced for any help!

    Tom
    T, Oct 10, 2008
    #2
    1. Advertising

  3. T

    Ben Morrow Guest

    Quoth T <>:
    >
    > in a bash shell script I can do:
    >
    > eval `/cae/Modules/${MODULE_VERSION}/bin/modulecmd bash load
    > myproject`
    >
    > and this works, I tried the equivlant in perl:
    >
    > eval `/cae/Modules/$ENV{'MODULE_VERSION'}/bin/modulecmd tcsh load
    > myproject`
    >
    > along with several other variants without any luck. I was hoping to
    > avoid writing
    > a shell wrapper around my Perl script. Does anyone know how to load
    > the shell environment
    > in a Perl script? This script will run in crontab file that's why I
    > need to load the env.


    In general there isn't any simple way. If the output of `modulecmd` is
    simple, perhaps something like

    export FOO="BAR"
    export BAZ="QUUX"

    without any tricky shell syntax, you could parse it in Perl and insert
    the results into %ENV. Otherwise, you will need a shell wrapper.

    Ben

    --
    For far more marvellous is the truth than any artists of the past imagined!
    Why do the poets of the present not speak of it? What men are poets who can
    speak of Jupiter if he were like a man, but if he is an immense spinning
    sphere of methane and ammonia must be silent? [Feynmann]
    Ben Morrow, Oct 10, 2008
    #3
  4. T <> wrote:
    >a shell wrapper around my Perl script. Does anyone know how to load
    >the shell environment
    >in a Perl script? This script will run in crontab file that's why I
    >need to load the env.


    This is a variation of 'perldoc -q environment'.
    The `...` starts a child process, runs the modulecmd command in that
    child process, and then throws away the results because parent processes
    don't inherit environment changes from their children.

    jue
    Jürgen Exner, Oct 10, 2008
    #4
  5. T

    smallpond Guest

    On Oct 10, 11:26 am, T <> wrote:
    > On Oct 10, 11:22 am, T <> wrote:> Greetings:
    >
    > > We use Modules ( I hate that they chose that name) to load our
    > > shell environment here. So I can do:

    >
    > > mod load myproject

    >
    > > "mod" is an alias to:

    >
    > Sorry about that...
    > geting back
    >
    > /cae/Modules/${MODULE_VERSION}/bin/modulecmd bash load myproject
    >
    > in a bash shell script I can do:
    >
    > eval `/cae/Modules/${MODULE_VERSION}/bin/modulecmd bash load
    > myproject`
    >
    > and this works, I tried the equivlant in perl:
    >
    > eval `/cae/Modules/$ENV{'MODULE_VERSION'}/bin/modulecmd tcsh load
    > myproject`
    >
    > along with several other variants without any luck. I was hoping to
    > avoid writing
    > a shell wrapper around my Perl script. Does anyone know how to load
    > the shell environment
    > in a Perl script? This script will run in crontab file that's why I
    > need to load the env.
    >
    > Thanks in Advanced for any help!
    >
    > Tom



    1) Don't use pronouns without a referent. Nobody here knows who
    "they" are.

    2) perl is run from a shell, it doesn't run the shell. If you try to
    start another shell from perl
    it will end when the perl script does.

    3) perl eval executes perl code, not shell code. Calling a function
    without reading the documentation
    because the name sounds right and it might do what you want is derided
    as "Cargo Cult Programming"

    4) there is no CPAN code for improving your luck. Feel free to write
    back when you understand how to
    check the error status from calls and can describe an actual error.
    Make sure you have these two
    lines at the beginning of your script:

    use warnings;
    use strict;
    smallpond, Oct 10, 2008
    #5
  6. T

    Jamie Guest

    In <>,
    T <> mentions:
    >Greetings:
    >
    > We use Modules ( I hate that they chose that name) to load our
    >shell environment here. So I can do:
    >
    > mod load myproject
    >
    > "mod" is an alias to:
    >
    >/cae/Modules/${MODULE_VERSION}/bin/modulecmd bash load hagrid


    And you need the environment from the child shell?

    UNIX doesn't work that way, each child process inherits the environment from
    the parent. A child process can't change the environment of the parent. (this
    is why "cd" is a shell built in) Believe it or not, this is a feature of unix.
    (just imagine if a child process could change your path so that the 'cp'
    command is now the 'rm' command!)

    When you "source" a shell script, you're just telling the shell to run the
    script in the current process.

    If you're using linux, you can get the environment in a highly non-portable way
    using the "proc" filesystem.

    To see it try this:

    cat /proc/$PID/environ | tr "\0" "\n"

    (You need permission..) Of course, the process needs to be running for it
    to work...

    This isn't portable at all!

    Probably your best bet is to have a shell wrapper emit the environment to
    stdout and parse the results in perl.


    Jamie
    --
    http://www.geniegate.com Custom web programming
    Perl * Java * UNIX User Management Solutions
    Jamie, Oct 10, 2008
    #6
  7. On 2008-10-10 16:23, Jürgen Exner <> wrote:
    > T <> wrote:


    [ you snipped the relevant part of the posting which included

    eval `/some/command bash other parameters`
    eval `/some/command tcsh other parameters`

    ]

    >>a shell wrapper around my Perl script. Does anyone know how to load
    >>the shell environment
    >>in a Perl script? This script will run in crontab file that's why I
    >>need to load the env.

    >
    > This is a variation of 'perldoc -q environment'.
    > The `...` starts a child process, runs the modulecmd command in that
    > child process, and then throws away the results because parent processes
    > don't inherit environment changes from their children.


    No. The child process doesn't set the environment. The child process
    prints commands (presumably in "bash" or "tcsh" syntax, guessing from
    the parameters), which are then evaled by the parent process to set the
    environment.

    The problem is of course that Perl is a different language from both
    bash and tcsh and therefore cannor eval bash or tcsh commands. The child
    process would need to spit out perl statements for that to work. Maybe
    it can be modified so that

    eval `/some/command perl other parameters`

    does the right thing. Otherwise, use Ben's approach (actually, I would
    prefer Ben's approach anyway).

    hp
    Peter J. Holzer, Oct 11, 2008
    #7
  8. "Peter J. Holzer" <> wrote:
    >On 2008-10-10 16:23, Jürgen Exner <> wrote:
    >> T <> wrote:

    >
    >[ you snipped the relevant part of the posting which included
    >
    > eval `/some/command bash other parameters`
    > eval `/some/command tcsh other parameters`
    >
    >]
    >
    >>>a shell wrapper around my Perl script. Does anyone know how to load
    >>>the shell environment
    >>>in a Perl script? This script will run in crontab file that's why I
    >>>need to load the env.

    >>
    >> This is a variation of 'perldoc -q environment'.
    >> The `...` starts a child process, runs the modulecmd command in that
    >> child process, and then throws away the results because parent processes
    >> don't inherit environment changes from their children.

    >
    >No. The child process doesn't set the environment. The child process
    >prints commands (presumably in "bash" or "tcsh" syntax, guessing from
    >the parameters), which are then evaled by the parent process to set the
    >environment.


    You are right, I misread the problem. Thanks for pointing this out.

    jue
    Jürgen Exner, Oct 11, 2008
    #8
  9. Joe Smith wrote:
    > T wrote:
    >
    >> eval `/cae/Modules/${MODULE_VERSION}/bin/modulecmd bash load
    >> myproject`
    >>
    >> and this works, I tried the equivlant in perl:
    >>
    >> eval `/cae/Modules/$ENV{'MODULE_VERSION'}/bin/modulecmd tcsh load
    >> myproject`
    >>
    >> Does anyone know how to load the shell environment
    >> in a Perl script? This script will run in crontab

    >
    > In bash, the syntax is something like:
    >
    > PATH=$PATH:/usr/local/bin:$HOME/bin
    >
    > In perl, the equivalent would be:
    >
    > $ENV{PATH} = "$ENV{PATH}:/usr/local/bin:$ENV{HOME}/bin";
    > or
    > $ENV{PATH} .= ":/usr/local/bin:$ENV{HOME}/bin";


    Or:

    use Env qw/@PATH $HOME/;
    push @PATH, '/usr/local/bin', "$HOME/bin";



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
    John W. Krahn, Oct 12, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    5,664
    Jonathan Bromley
    Feb 18, 2005
  2. Christian Heimes
    Replies:
    0
    Views:
    589
    Christian Heimes
    Feb 27, 2008
  3. Gerardo Herzig
    Replies:
    1
    Views:
    1,060
    Philipp Pagel
    Feb 27, 2008
  4. D'Arcy J.M. Cain
    Replies:
    0
    Views:
    850
    D'Arcy J.M. Cain
    Feb 27, 2008
  5. Samuel A. Falvo II
    Replies:
    0
    Views:
    475
    Samuel A. Falvo II
    Oct 8, 2008
Loading...

Share This Page