473,320 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

putenv

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

Dec 20 '05 #1
7 2609
On 2005-12-20, dp******@gmail.com <dp******@gmail.com> wrote:
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).

--
Grant Edwards
gr****@visi.com
Dec 20 '05 #2
On Tue, 20 Dec 2005 05:35:48 -0000
Grant Edwards <gr****@visi.com> wrote:
On 2005-12-20, dp******@gmail.com <dp******@gmail.com>
wrote:
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).


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

--
Terry Hancock (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Dec 20 '05 #3
Terry Hancock wrote:
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

Dec 20 '05 #4
Terry Hancock <ha*****@anansispaceworks.com> writes:
On Tue, 20 Dec 2005 05:35:48 -0000
Grant Edwards <gr****@visi.com> wrote:
On 2005-12-20, dp******@gmail.com <dp******@gmail.com>
wrote:
> 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).


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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Dec 20 '05 #5
Mike Meyer wrote:
Terry Hancock <ha*****@anansispaceworks.com> writes:
On Tue, 20 Dec 2005 05:35:48 -0000
Grant Edwards <gr****@visi.com> wrote:
On 2005-12-20, dp******@gmail.com <dp******@gmail.com>
wrote:

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).


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.


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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 20 '05 #6
Mike Meyer a écrit :
Terry Hancock <ha*****@anansispaceworks.com> writes:
On Tue, 20 Dec 2005 05:35:48 -0000
Grant Edwards <gr****@visi.com> wrote:
On 2005-12-20, dp******@gmail.com <dp******@gmail.com>
wrote:

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).


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.


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.
Dec 20 '05 #7
On Tue, 20 Dec 2005, Steve Holden wrote:
Mike Meyer wrote:
Terry Hancock <ha*****@anansispaceworks.com> writes:
On Tue, 20 Dec 2005 05:35:48 -0000
Grant Edwards <gr****@visi.com> wrote:

On 2005-12-20, dp******@gmail.com <dp******@gmail.com>
wrote:

> 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.

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.


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

--
Hit to death in the future head
Dec 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Warren Oates | last post by:
I've been using putenv() to change the timezone in a script (my server is in CST, me in EST). My reading of the docs suggests that this changes the time zone environment variable for _the server_...
1
by: Bathroom_Monkey | last post by:
I've got a newb question about Daylight Savings Time (DST). First of all here is some server info: Apache 1.3.28 PHP 4.3.6 MySQL 3.23.49 Time is set to GMT My goal is to be able to...
1
by: Greg Lindstrom | last post by:
Hello - I am trying to connect to an Oracle database on an HP-9000 via Python 2.3 and cx_Oracle. I have set the following in my python routine: os.putenv('ORACLE_HOME', '/u01/app/oracle')...
4
by: techme | last post by:
I am surprised at how little info there is on this topic. We converted to DB2 v8.1.1.72 painlessly a few months ago. For specific reasons, we stayed on 32-bit. Our OS is AIX 5.2 and our hardward...
2
by: LeaFriend | last post by:
Hello. It's rainy day of winter in here. :( I install php on AIX, with OCI8 ext. WebServer is WebtoB, WAS is JEUS. There was a problem on compiling (oci.h is not found) and linking. (machine...
4
by: Yogi Watcher | last post by:
Hi, Recently I have observed some odd behavior of getenv and putenv function. I am developing some code that integrates with several other libraries. This program is not using MFC. It is plain C...
8
by: Michael B Allen | last post by:
Is the string returned by getenv guaranteed to be the same string supplied to putenv plus the offset of the variable name and equals sign? Because of API constraints I do not want to save a...
4
by: ClownPleco | last post by:
I have read several posts about putenv is not Standard C. But I'm wondering if anyone knows if it is available in AIX. All of our other platforms (linux (32 and 64 bit), sgi, hpux, x86, darwin...
4
by: google | last post by:
>>> 'C:\\WINNT\\system32;C:\\WINNT;C:\\WINNT\\System32\\Wbem;%C:\\WINNT%\ \system32;%C:\\WINNT%;%C:\\WINNT%\\System32\\Wbem' 'C:\\WINNT\\system32;C:\\WINNT;C:\\WINNT\\System32\\Wbem;%C:\\WINNT%\...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.