How to capture environment state after running a shell script.

G

Gerard Flanagan

Hello,

I have a third party shell script which updates multiple environment
values, and I want to investigate (and ultimately capture to python)
the environment state after the script has run. But running the script
as a child process only sets values for that process, which are lost
after execution. So I thought I could simply tack on an 'env' command
line to the script input lines as shown below. However, using
subprocess.Popen gives the error shown (even though the docs say that
any file object may be used for stdin), and using popen2 hangs
indefinitely. I think I'm missing something basic, any advice? Or is
there a better approach?

(This is Python 2.4 and a Korn shell script on AIX Unix)

Thanks in advance.

from StringIO import StringIO
from subprocess import Popen
from popen2 import popen2

fname = '/opt/WebSphere/AppServer/bin/setupCmdLine.sh'

buf = StringIO()

f = open(fname, 'r')

try:
f.readline() #ignore shebang
for line in f:
buf.write(line)
finally:
f.close()

buf.write('\nenv\n')

buf.seek(0)

########## first method ##########
p = Popen('/bin/sh', stdin=buf)
print p.stdout.readlines()

Traceback (most recent call last):
File "scratch.py", line 36, in ?
p = Popen('/bin/sh', stdin=buf)
File "/usr/local/lib/python2.4/subprocess.py", line 534, in __init__
(p2cread, p2cwrite,
File "/usr/local/lib/python2.4/subprocess.py", line 830, in
_get_handles
p2cread = stdin.fileno()
AttributeError: StringIO instance has no attribute 'fileno'

########## second method ##########
cmdout, cmdin = popen2('/bin/sh')
for line in buf:
cmdin.write(line)

ret = cmdout.readlines()
cmdout.close()
cmdin.close()

print ret
 
A

attn.steven.kuo

Hello,

I have a third party shell script which updates multiple environment
values, and I want to investigate (and ultimately capture to python)
the environment state after the script has run. But running the script
as a child process only sets values for that process, which are lost
after execution. So I thought I could simply tack on an 'env' command
line to the script input lines as shown below. However, using
subprocess.Popen gives the error shown (even though the docs say that
any file object may be used for stdin), and using popen2 hangs
indefinitely. I think I'm missing something basic, any advice? Or is
there a better approach?

(snipped)

########## first method ##########
p = Popen('/bin/sh', stdin=buf)
print p.stdout.readlines()

Traceback (most recent call last):
File "scratch.py", line 36, in ?
p = Popen('/bin/sh', stdin=buf)
File "/usr/local/lib/python2.4/subprocess.py", line 534, in __init__
(p2cread, p2cwrite,
File "/usr/local/lib/python2.4/subprocess.py", line 830, in
_get_handles
p2cread = stdin.fileno()
AttributeError: StringIO instance has no attribute 'fileno'

########## second method ##########
cmdout, cmdin = popen2('/bin/sh')
for line in buf:
cmdin.write(line)

ret = cmdout.readlines()
cmdout.close()
cmdin.close()

print ret



First close the input so that the (sub) process
knows to terminate and flush the output. Then,
you can read from the output:

import subprocess
import popen2

p = subprocess.Popen(["/bin/sh"], stdin=subprocess.PIPE,
stdout=subprocess.PIPE)

p.stdin.write("env -i FOO=BAR\n")
p.stdin.close()
status = p.wait()
ret = p.stdout.readlines()
p.stdout.close()

print ret

# Or

cmdout, cmdin = popen2.popen2("/bin/sh")
cmdin.write("env -i FOO=BAR\n")
cmdin.close()
ret = cmdout.readlines()
cmdout.close

print ret
 
G

Gerard Flanagan

I have a third party shell script which updates multiple environment
values, and I want to investigate (and ultimately capture to python)
the environment state after the script has run.

First close the input so that the (sub) process
knows to terminate and flush the output. Then,
you can read from the output:

import subprocess
import popen2

p = subprocess.Popen(["/bin/sh"], stdin=subprocess.PIPE,
stdout=subprocess.PIPE)

p.stdin.write("env -i FOO=BAR\n")
p.stdin.close()
status = p.wait()
ret = p.stdout.readlines()
p.stdout.close()


print ret

Perfect! Works a charm. Thanks for helping me out.

Gerard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top