473,396 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

getting env variable from bourne shell script

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
2 2254
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
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...
11
by: Paul D.Smith | last post by:
Can Python create a variable "on-the-fly". For example I would like something like... make_variable('OSCAR', 'the grouch'); print OSCAR; ....to output... the grouch
28
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...
2
by: Curtiosity | last post by:
I have done a create or replace view called creditcard1. If I do a "select * from creditcard1" it retrieves the data just fine. If I try to do a statement where I am listing the column names it...
7
by: Brian Tkatch | last post by:
This is a shell question (bash), but i'm more familiar with this group, so i hope noone minds. I have a bash shell script which needs to query the database and return information into a...
1
by: nanosuna | last post by:
Hi, I was given a task to translate a unix bourne shell script that does file renaming and string replacement on a server . I am not too familiar with the capabilites of JavaScript, so I was...
1
by: Rafael Fernandez | last post by:
Hi folks, I have the following sql/pl: CREATE PROCEDURE DBSTG.TEST (OUT PRESULT INTEGER ) SPECIFIC DBSTG.TEST DYNAMIC RESULT SETS 1 LANGUAGE SQL
2
crystal2005
by: crystal2005 | last post by:
Hi all, I have been googling to search an example code of archiving (E.g. .tgz .tar .zip) for my research. However, unluckily i found none. Is there anyone can help to find one? Oh yeah, I'm...
0
by: Cameron Simpson | last post by:
On 17Aug2008 21:25, John Nagle <nagle@animats.comwrote: Because $HOSTNAME is a bash specific variable, set by bash but NOT EXPORTED! Like $0 and a bunch of other "private" variables, subprocesses...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.