[Tutor] Re[2]: [Tutor] ksh and SAS questions

Guido van Rossum guido@CNRI.Reston.VA.US
Fri, 04 Jun 1999 17:09:00 -0400


> From: David Ascher <da@ski.org>

> On Fri, 4 Jun 1999 adeline_j_wilcox@ccMail.Census.GOV wrote:
> 
> >      Well, I can't see the error in the following script. Running 
> >      SunOS 5.5.1. When I run my Python script, I get this error 
> >      message:
> >      
> > Traceback (innermost last):
> >   File "recheck.py", line 14, in ?
> >     month = os.environ['CATI']
> >   File "/usr/local/lib/python1.5/UserDict.py", line 12, in __getitem__
> >     def __getitem__(self, key): return self.data[key]
> > KeyError: CATI
> 
> This means that there is no environment variable called CATI when you run
> line 14 -- if that was supposed to be set by
> 
> >     12  os.system('/op/ctl/prod/scripts/setcm.ksh')
> 
> then there's something wrong with that script.  I don't know the Korn
> shell, but it could be that you need to specify something in that shell to
> make the environment variables "stick" after execution.  In bash, I
> believe it's the "export" command.

Continuing David's assumption that this script is supposed to set the
CATI env.variable, calling the script using os.system() will never
work: it is "child process" (in Unix terminology) and a child process
can never change an environment variable of the parent (here the
executing Python program).  One solution is to run the script
(manually) *before* the Python program.  Another might be to get the
script to print the value of CATI and suck it out of there using
os.popen(); I'll leave it to another tutor to explain how to do this
in more detail.  Hint: maybe this would work:

 output = os.popen('/op/ctl/prod/scripts/setcm.ksh; echo $CATI').read()
 month = string.strip(OUTPUT)

--Guido van Rossum (home page: http://www.python.org/~guido/)