473,387 Members | 1,510 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,387 software developers and data experts.

Setting environment variables for tcsh session

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
3 2948
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rick Kasten | last post by:
Here's what I need to do: 1) Write "Password:" to the console - I know how to do that 2) Accept input that is not written back to the console - I know how to do that (Term::ReadKey) 3) Set the...
1
by: SG | last post by:
I use the %ENV to access the environment variables. I came across the following problem. When I change the environment variable from command line, i.e let us say origianlly env var $FOO was bar1,...
4
by: Bill Davidson | last post by:
All: I've found the 'Environment.GetEnvironmentVariable()' method; but how do I create and/or set an environment variable? Thanks, Bill
5
by: pinkfloydhomer | last post by:
I have a Makefile target that uses a python script, like: %.abc: %.def python myscript.py The problem is that myscript.py and some modules that myscript.py imports are not in the current...
6
by: Fuzzyman | last post by:
Hello all, I would like to set a Windows Environment variable for another (non-child) process. This means that the following *doesn't* work : :: os.environ = value In the ``win32api``...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
8
by: Jack Vamvas | last post by:
I know that you can do : Request.ServerVariables("QUERY_STRING") but can you actually set a serverVariable? I know that sounds counter intuitive , but is it possible, via vbscipt/asp? -- ...
5
by: Henaro | last post by:
Hello~ I am having trouble setting environment variables in C++ on win32. The code that is not working is: char prxy; char pf_cmd1 = "set http_proxy="; ....
2
by: bart.hernalsteen | last post by:
Hi, I want to read the envorinment setting "path" and then on certain conditions change this setting. ex path : "%SystemRoot%\system32;%SystemRoot%;%SystemRoot% \System32\Wbem;C:\Program...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.