Setting UNIX environment with a Python script
James T. Dennis
jadestar at idiom.com
Thu Mar 14 21:09:27 EST 2002
Cameron Hutchison <camh at zip.com.au> wrote:
> On Tue, 12 Mar 2002 13:25:57 +1100, Grant Edwards wrote:
>> In article <pan.2002.03.12.11.14.20.48643.1649 at zip.com.au>, Cameron
>> Hutchison wrote:
>>> What you need to do instead is have your python program print on stdout
>>> the name of the ora_env file to source. Then you invoke your python
>>> script from your shell login script like this (bourne style shell
>>> assumed):
>>> eval "source ora_env.`get_ora_name`"
>> 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.
So the choice of implementation for `foo` is not constrained
by the shell under which it is run (once could offer a command
line argument to support alternative shells: eval `foo -s csh`
for example). Since it's not constrained by this; it should
logically be chosen on other criteria, such as SUITABILITY TO
THE JOB AT HAND! (Doh!)
If the OP feels that the parsing and processing that he or she
requires is best done in Python, then Python is just as good
at printing "FOO=BAR" and "export FOO" (Bourne, et al) or
"set FOO=BAR" and "setenv FOO" (csh) as any other language.
More information about the Python-list
mailing list