getting env variable from bourne shell script

B

biner

Hello,

I have file called PARAMETRES that is used in bourne shell script to
define variable. In order to do so I put a ". PARAMETRES" line and the
script has acces to all the variable defined in the PARAMETRES file.

Now, I would like to be able to get the same thing in python. I
googled and played with os.system to try to come up with something but
no success so far. I also tryed exec but it doesn't work becaus a lot
of string variable difined in the PARAMETRES file do not have the '
sign needed in python string.

for example. Let's say I have a file PARAMETRES containing
TOTO=whatever, I would like to have a command to read that file and
have the variable TOTO='whatever' in python.

Thanks for any help.

Sébastien
 
T

Thomas Guettler

Am Tue, 21 Dec 2004 07:51:53 -0800 schrieb biner:
Hello,

I have file called PARAMETRES that is used in bourne shell script to
define variable. In order to do so I put a ". PARAMETRES" line and the
script has acces to all the variable defined in the PARAMETRES file.

Hi,

you can run ". PARAMETERS; set" with popen. Then read stdin and
parse the lines:

The lines from stdin look like this:

ACLOCAL_PATH=/opt/gnome/share/aclocal
BASH=/bin/bash
BASH_VERSINFO=([0]="2" [1]="05b" [2]="0" [3]="1" [4]="release" [5]="i586-suse-linux")
BASH_VERSION='2.05b.0(1)-release'
COLORTERM=1
COLUMNS=143
CPU=i686
CVSROOT=:ext:tguettler@homeserver:/raid/develop
CVS_RSH=ssh
....

you could use '^(.*?)=(.*)$' as regex to parse each line
group(1) is the variable name group(2) the value.

HTH,
Thomas
 
F

Fredrik Lundh

Thomas said:
you could use '^(.*?)=(.*)$' as regex to parse each line
group(1) is the variable name group(2) the value.

may I recommend:

key, value = line.split("=", 1)

</F>
 

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