Loading the shell environment?

T

T

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

T

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
 
B

Ben Morrow

Quoth T said:
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
 
J

Jürgen Exner

T said:
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
 
S

smallpond

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;
 
J

Jamie

In said:
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
 
P

Peter J. Holzer


[ you snipped the relevant part of the posting which included

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

]
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
 
J

Jürgen Exner

Peter J. Holzer said:

[ you snipped the relevant part of the posting which included

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

]
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

John W. Krahn

Joe said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top