473,770 Members | 6,158 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 2851
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
2287
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
2228
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
3820
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
2222
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
2968
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
20997
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
2948
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
2471
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
2555
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
9592
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
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
10231
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
10059
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...
0
9871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3972
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
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.