473,883 Members | 1,685 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to capture environment state after running a shell script.

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.Pope n 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('\nen v\n')

buf.seek(0)

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

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(lin e)

ret = cmdout.readline s()
cmdout.close()
cmdin.close()

print ret

Mar 13 '07 #1
2 2859
On Mar 13, 5:57 am, "Gerard Flanagan" <grflana...@yah oo.co.ukwrote:
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.Pope n 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.readli nes()

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(lin e)

ret = cmdout.readline s()
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.Pope n(["/bin/sh"], stdin=subproces s.PIPE,
stdout=subproce ss.PIPE)

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

print ret

# Or

cmdout, cmdin = popen2.popen2("/bin/sh")
cmdin.write("en v -i FOO=BAR\n")
cmdin.close()
ret = cmdout.readline s()
cmdout.close

print ret

--
Hope this helps,
Steven

Mar 13 '07 #2
On Mar 13, 7:54 pm, attn.steven.... @gmail.com wrote:
On Mar 13, 5:57 am, "Gerard Flanagan" <grflana...@yah oo.co.ukwrote:
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.

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.Pope n(["/bin/sh"], stdin=subproces s.PIPE,
stdout=subproce ss.PIPE)

p.stdin.write(" env -i FOO=BAR\n")
p.stdin.close()
status = p.wait()
ret = p.stdout.readli nes()
p.stdout.close( )
print ret
Perfect! Works a charm. Thanks for helping me out.

Gerard
Mar 14 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
2296
by: Ville Vainio | last post by:
Pythonic Nirvana - towards a true Object Oriented Environment ============================================================= IPython (by Francois Pinard) recently (next release - changes are still in CVS) got the basic abilities of a system shell (like bash). Actually, using it as a shell is rather comfortable. This all made me think... Why do we write simple scripts to do simple things? Why do we serialize data to flat text files in...
3
2232
by: Greg Lindstrom | last post by:
Hello- I am running python 2.3. on an HP-9000 box running Unix and have a POSIX script that sets up my production environment. I would like to run the script from inside a python routine and pick up the environment variables but am having trouble getting it done. Suppose the script is called setprod. How can I call this from inside a python script then pick up the env's later in my program? I've tried os.system ('setprod') and...
1
3827
by: Pawel Banys | last post by:
Hello, There is an operation which can be performed at the shell prompt the following way: some_prg > result_file However, before "some_prg" can use files, they have to be converted and it can be done like this:
4
2227
by: Vivek Chaudhary | last post by:
Is it possible to set an environment variable in python script whose value is retained even after the script exits. Doing the following creates an environment variable "name" which is visible to only subprocesses created by os.system() and os.popen(). os.putvar("name", "vivek") Is it possible to somehow create this environment variable inside python script which will be avaibale even after the script exits. In
3
2973
by: David Durkee | last post by:
Hi, I'm trying to write a script I can run from tcsh in Terminal (on Mac OS X) that will set environment variables that can be accessed by subsequent commands I execute in that session. Not having any luck so far. Here's what I've tried: ------------ #!/usr/bin/python
28
21016
by: Christian | last post by:
Another question from a not even newbie: In Unix you can set an environment variable with the command export PYTHONPATH but I would like to set the variable from at .py script. So my question is: How do I export an environment variable in a .py script?
4
2955
by: Richard Levasseur | last post by:
(Forewarning, most of these problems and solutions come from being the only developer in a 1 server department with no budget, few resources, unresponsive IT, and non-technical managers, so thats where I'm coming from.) (Additionally, this may or may not fit in this group, but I know there's bright people here, and it is largely PHP development centric) Any time I've done web development, I've always been plagued by some of the...
39
2489
by: Alan Isaac | last post by:
This may seem very strange, but it is true. If I delete a .pyc file, my program executes with a different state! In a single directory I have module1 and module2. module1 imports random and MyClass from module2. module2 does not import random. module1 sets a seed like this::
1
2559
by: replysonika | last post by:
Hello, I run a Java app with subprocess from Python script. This python script is called from another Python Wrapper. python = subprocess.Popen(, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
0
9779
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11114
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10730
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10835
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5787
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.