Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Loading the shell environment?

1 view
Skip to first unread message

T

unread,
Oct 10, 2008, 11:22:00 AM10/10/08
to
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

unread,
Oct 10, 2008, 11:26:47 AM10/10/08
to
On Oct 10, 11:22 am, T <g41...@motorola.com> 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

Ben Morrow

unread,
Oct 10, 2008, 12:17:15 PM10/10/08
to

Quoth T <g41...@motorola.com>:

>
> 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] b...@morrow.me.uk

Jürgen Exner

unread,
Oct 10, 2008, 12:23:21 PM10/10/08
to
T <g41...@motorola.com> 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

smallpond

unread,
Oct 10, 2008, 12:26:56 PM10/10/08
to


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;

Jamie

unread,
Oct 10, 2008, 5:51:59 PM10/10/08
to
In <bac5321a-9817-4f3a...@h60g2000hsg.googlegroups.com>,
T <g41...@motorola.com> mentions:

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

Peter J. Holzer

unread,
Oct 11, 2008, 3:25:15 AM10/11/08
to
On 2008-10-10 16:23, Jürgen Exner <jurg...@hotmail.com> wrote:
> T <g41...@motorola.com> wrote:

[ you snipped the relevant part of the posting which included

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

]

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

Jürgen Exner

unread,
Oct 11, 2008, 4:28:12 AM10/11/08
to
"Peter J. Holzer" <hjp-u...@hjp.at> wrote:
>On 2008-10-10 16:23, Jürgen Exner <jurg...@hotmail.com> wrote:
>> T <g41...@motorola.com> 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

Joe Smith

unread,
Oct 11, 2008, 8:36:05 PM10/11/08
to
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";

If `modulecmd` were to be modified so that it can produce perl syntax
as well, then you can use this:

eval `/cae/Modules/$ENV{MODULE_VERSION}/bin/modulecmd perl load myproject`;

-Joe

John W. Krahn

unread,
Oct 12, 2008, 12:03:46 AM10/12/08
to
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

Big and Blue

unread,
Oct 12, 2008, 7:48:01 AM10/12/08
to
> T wrote:
>
> Does anyone know how to load the shell environment
> in a Perl script? This script will run in crontab

It's a shell environment. It's meant for loading into a shell. So load
it into a shell.

Write a simple script to load the env and then exec perl.

e.g:

==========
#!/bin/sh
#
mod load myproject
exec perl-command

==========

or a more generic one, where you give the real command as the parameters

==========
#!/bin/sh
#
mod load myproject
exec ${1+"$@"}
==========


--
Just because I've written it doesn't mean that
either you or I have to believe it.

0 new messages