By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,396 Members | 1,442 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,396 IT Pros & Developers. It's quick & easy.

getting env variable from bourne shell script

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

--
Thomas Güttler, http://www.thomas-guettler.de/
Jul 18 '05 #2

P: n/a
Thomas Guettler wrote:
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>

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.