Setting environment variables for tcsh session

D

David Durkee

Hi,

I'm trying to write a script I can run from tcsh in Terminal (on Mac
OS X) that will set environment variables that can be accessed by
subsequent commands I execute in that session. Not having any luck so
far. Here's what I've tried:

------------
#!/usr/bin/python

import sys
import commands
import os

inputs = sys.argv[1:]
if len(inputs) == 2:
key = inputs[0]
val = inputs[1]

cmd = 'setenv %s "%s"' % (key, val)
print cmd
print commands.getstatusoutput(cmd)
print commands.getoutput('echo $' + key)

-------------
I named the script pysetenv and executed it with the command:

pysetenv TEST "This is a test"

This prints the command in the form I intended, the tcsh setenv
command. When it executes the command, however, it gets an error that
makes it apparent that it is running the command in the sh shell
instead of the tcsh shell I ran it from. So I changed the format of
the command to that used by sh, making the last four lines of my
script:

cmd = "%s='%s'; export %s" % (key, val, key)
print cmd
print commands.getstatusoutput(cmd)
print commands.getoutput('echo $' + key)

For this one it was apparent that the command did not get an error,
but the echo was still echoing an old value of the variable.

So I replaced the last four lines with this:

os.environ[key] = val
print commands.getoutput('echo $' + key)

In this case, the echo command printed the value of the variable I had
just set! Success, or so I thought. However, a subsequent echo command
from the command prompt still returned the old value of the variable.
I think running the script must start a new session and that variables
set in that session are only current as long as the script is running.

Any ideas?

David
 
D

Dan Sommers

I'm trying to write a script I can run from tcsh in Terminal (on Mac
OS X) that will set environment variables that can be accessed by
subsequent commands I execute in that session. Not having any luck so
far. Here's what I've tried:

By design, you can't do that (think of the problems if arbitrary
programs could change your interactive shell's environment behind your
back).

Processes (your script) cannot change the environment of their parents
(your shell). The best you can do is have your script output the right
commands and have your shell read them. See "Command Substitution" in
the tcsh man page.

Regards,
Dan
 
D

David Durkee

I didn't get much from the discussion of Command Substitution. Any
tips on how to do that?

David
 
D

Dan Sommers

I didn't get much from the discussion of Command Substitution. Any tips
on how to do that?

I don't use tcsh, so there may be some subtleties I don't get, but it's
going to look something like this:

----------------changefoo.py
print 'setenv foo bar'
----------------end of changefoo.py

Then the shell session would look like this:

% `python changefoo.py`
% echo $foo
bar

or you may need to "eval" the output:

% eval `python changefoo.py`
$ echo $foo
bar

or you could it like this:

----------------changefoo.py:
print 'foo bar'
----------------end of changefoo.py

Then the shell session would look like this:

% setenv `python changefoo.py`
% echo $foo
bar

But we're veering dangerously off topic for c.l.python.

HTH,
Dan
 

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,982
Messages
2,570,186
Members
46,743
Latest member
WoodrowMea

Latest Threads

Top