Setting UNIX environment with a Python script

Donn Cave donn at drizzle.com
Fri Mar 15 01:11:38 EST 2002


Quoth "James T. Dennis" <jadestar at idiom.com>:
| Cameron Hutchison <camh at zip.com.au> wrote:
|> On Tue, 12 Mar 2002 13:25:57 +1100, Grant Edwards wrote:
...
|>> Another common method is to have your Python program write the shell
|>> commands to stdout, then execute them with something like
|
|>> eval `myPythonProgram`
|
|> While that would work fine, I dont think it's a terribly good idea. I
|> think it would be cleaner to keep the shell syntax in a shell script and
|> avoid having the python program assume the scripting environment it is
|> returning to.
|
| 	Having "myPythonProgram" implemented in ANY language (including
| 	in ANY shell)  makes exactly the same assumptions: to wit:
| 	FOO=BAR assigns shell variables and either export (Bourne & family)
| 	or setenv (csh/tcsh) exports them into the environment.
|
| 	*ANY* program or script that is intended to be invoked under 
| 	eval `....` will have to make those assumptions.  

Yes, and that's why it's not really an ideal design!  I think that's
his point - don't write your tools to be invoked in an eval statement,
if you don't have to.  Or "source", or any solution that requires the
tool to know the natural syntax of its invoker.

How would we write it instead, to avoid this problem?  If there's CPU
time to spare, one alternative might be to invoke python separately for
each variable, and in that case python can print out just the value for
that variable.

  sh:   EX1="`exsetup EX1`" export EX1
  rc:   EX1 = `{exsetup EX1}
  ... etc.

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list