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
/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
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
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
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;
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
[ 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
You are right, I misread the problem. Thanks for pointing this out.
jue
> 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
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
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.