By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,111 Members | 1,580 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,111 IT Pros & Developers. It's quick & easy.

Setting environment variables for tcsh session

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Thu, 21 Jul 2005 21:39:01 -0500,
David Durkee <dd*****@gmail.com> wrote:

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

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Jul 22 '05 #2

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

David

On Jul 21, 2005, at 10:12 PM, Dan Sommers wrote:

On Thu, 21 Jul 2005 21:39:01 -0500,
David Durkee <dd*****@gmail.com> wrote:

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

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
--
http://mail.python.org/mailman/listinfo/python-list

Jul 22 '05 #3

P: n/a
On Fri, 22 Jul 2005 10:56:22 -0500,
David Durkee <dd*****@gmail.com> wrote:
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

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.