UNIX Shell Interaction

Donn Cave donn at u.washington.edu
Fri Aug 18 12:57:34 EDT 2000


Quoth "Bell Atlantic" <apjewell at bigfoot.com>:
| Ugh - the nemesis!  I've done quite a bit shelling out from Python scripts
| and call me stupid, but it's a a real PITA to get a return value that you
| want.  The problem is (and hopefully my inadequate response will prompt
| someone who actually KNOWS to answer;) - you're SHELLING out to a script
| that, itself, starts a shell (in the first place - also might explain why
| your environment variables disappear).  In the second place - if the script
| doesn't explicitly return a value, even if it WERE to fail - it would most
| likely return 0 to Python because the shell ITSELF was successfully launched
| and concluded (regardless of the practical conclusion of the script (i.e.,
| "(shell): uhhh, I exemacuted and there's no smoke - I must have passed!").
| Sooooo. . .  I hope someone else chimes in but for my purposes, I always
| write to a file in the Unix script and parse it in the Python script to
| determine the true exit conditions.  Would appreciate an update here if you
| get a better answer offline!

Bjorn Pettersen's followup, which you quoted here, had the right answer
for the original question  - "try printing the return value".  The
`` (backward quote) feature of the Bourne shell captures output, so
the command just needs to generate output.

Your problem appears to be similar, but where the original question
asked how to return a value from Python to the shell, you seem to need
the shell script to return its output to Python.  Try
result = os.popen('ls /tmp', 'r').readlines() for starters.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list