putenv

D

dpugmire

Is there a trick to getting putenv/getenv to work?
I have csh script that calls a bunch of python programs and I'd like to
use env variables as kind of a global variable that I can pass around
to the pythong scripts.

Thanks,
Dave
 
G

Grant Edwards

Is there a trick to getting putenv/getenv to work?
No.

I have csh script that calls a bunch of python programs and I'd like to
use env variables as kind of a global variable that I can pass around
to the pythong scripts.

You can't change the environment of the parent process.

IOW, the python programs can't change the environment of the
calling csh script (or, by extenstion, the environment of
subsequent python programs that are called by the csh script).
 
T

Terry Hancock

You can't change the environment of the parent process.

IOW, the python programs can't change the environment of
the calling csh script (or, by extenstion, the environment
of subsequent python programs that are called by the csh
script).

There is an evil trick, however:

Instead of setting the environment directly, have the python
program return csh code to alter the environment the way you
want, then call the python code by "sourcing" its output:

source `my_script.py`

This will cause the csh script to execute the code and set
the variables, which will then be visible to subsequent
Python scripts.

If you don't want your script to alter anything, then you
just return '' and nothing happens, otherwise you return
something like:

"""
setenv VKG1 spam
setenv VKG2 eggs
"""

It's ugly, but it does work -- I have had to use this
before in a production environment. Well, it's not really
any less advisable than scripting in csh to begin with. ;-)

Cheers,
Terry
 
D

Dennis Lee Bieber

You can't change the environment of the parent process.

IOW, the python programs can't change the environment of the
calling csh script (or, by extenstion, the environment of
subsequent python programs that are called by the csh script).

Unless one is using "global" environment variables under AmigaOS --
but then, one would not be using a csh script...

AmigaOS had two levels of environment variables; locals were stored
in a memory structure of the shell, globals were really files in the
ENV: directory (and AmigaOS had logical names for directory paths too...
sort of the best of both VMS and UNIX <G> ENV: => SYS:env/, SYS: => dh0:
(or hd0: depending on driver) if booting from hard disk partition, df0:
for floppy) [and that has skipped the fact that it also /used/ volume
names, unlike Windows]

Uh, back on subject... Changes to global environment variables meant
writing a value to the file ENV:<variable-name>, hence it did become
available to the parent, to other programs, and even to other shell
windows...
--
 
P

Peter Hansen

Terry said:
source `my_script.py`

It's ugly, but it does work -- I have had to use this
before in a production environment. Well, it's not really
any less advisable than scripting in csh to begin with. ;-)

It can even be made no-so-ugly with an alias.

alias my_script='source `my_script.py`'

or some such...

-Peter
 
M

Mike Meyer

Terry Hancock said:
There is an evil trick, however:

Instead of setting the environment directly, have the python
program return csh code to alter the environment the way you
want, then call the python code by "sourcing" its output:

source `my_script.py`

Does this actually work? It looks to me like you need two levels:
my_script.py creates a file, then outputs the name of the file, as the
csh source command reads commands from the file named as an argument.

To be able to output the commands directly, you'd need to use the eval
command, not the source command.
It's ugly, but it does work -- I have had to use this
before in a production environment. Well, it's not really
any less advisable than scripting in csh to begin with. ;-)

Doesn't matter what you're scripting in - you'll have to do some such
circumlocution to set the parent scripts environment variables.

<mike
 
S

Steve Holden

Mike said:
Does this actually work? It looks to me like you need two levels:
my_script.py creates a file, then outputs the name of the file, as the
csh source command reads commands from the file named as an argument.

To be able to output the commands directly, you'd need to use the eval
command, not the source command.




Doesn't matter what you're scripting in - you'll have to do some such
circumlocution to set the parent scripts environment variables.

I suspect the trick that Terry was thinking of was eval, not source. You
are correct in saying he'd need to create a file to source.

When I run

ssh-agent

I see:

SSH_AUTH_SOCK=/tmp/ssh-J0r9XFySTm/agent.5500; export SSH_AUTH_SOCK;
SSH_AGENT_PID=5796; export SSH_AGENT_PID;
echo Agent pid 5796;

The process number varies each time. If I run

eval `ssh-agent`

I see:

Agent pid 4364

(this is Cygwin, hence the funky process numbers). Now, of course, I can
see the variables in the current shell's environment:

sholden@bigboy /tmp
$ echo $SSH_AUTH_SOCK $SSH_AGENT_PID
/tmp/ssh-W6LEPi8wC0/agent.4152 4364

regards
Steve
 
C

Christophe

Mike Meyer a écrit :
Does this actually work? It looks to me like you need two levels:
my_script.py creates a file, then outputs the name of the file, as the
csh source command reads commands from the file named as an argument.

Should work, even in basic sh. IIRC, source means the shell should
execute the data itself. I've always used it with an intermediate file
but I wouldn't be surprised if there was a way to make it work with a
command output.
 
T

Tom Anderson

I suspect the trick that Terry was thinking of was eval, not source. You are
correct in saying he'd need to create a file to source.

True. The downside of eval is that it doesn't (well, in bash, anyway)
handle line breaks properly (for some value of 'properly') - it seems to
treat them as linear whitespace, not line ends. I was about to suggest:

source <(my_script.py)

As a way to use source to run the script's output, but that seems not to
work. I think <() might be a bashism anyway.

tom
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top