FAQ
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

Search Discussions

  • Dan Sommers at Jul 22, 2005 at 3:12 am

    On Thu, 21 Jul 2005 21:39:01 -0500, David Durkee 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
  • David Durkee at Jul 22, 2005 at 3:56 pm
    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 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
  • Dan Sommers at Jul 22, 2005 at 7:01 pm

    On Fri, 22 Jul 2005 10:56:22 -0500, David Durkee 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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 22, '05 at 2:39a
activeJul 22, '05 at 7:01p
posts4
users2
websitepython.org

2 users in discussion

Dan Sommers: 2 posts David Durkee: 2 posts

People

Translate

site design / logo © 2022 Grokbase