getting env variable from bourne shell script

Discussion in 'Python' started by biner, Dec 21, 2004.

  1. biner

    biner Guest

    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
    biner, Dec 21, 2004
    #1
    1. Advertising

  2. 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/
    Thomas Guettler, Dec 21, 2004
    #2
    1. Advertising

  3. 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>
    Fredrik Lundh, Dec 21, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan Stromberg

    bourne shell parser in python

    Dan Stromberg, Apr 8, 2005, in forum: Python
    Replies:
    0
    Views:
    358
    Dan Stromberg
    Apr 8, 2005
  2. Christian Heimes
    Replies:
    0
    Views:
    583
    Christian Heimes
    Feb 27, 2008
  3. Replies:
    3
    Views:
    135
    Darren Dunham
    Jan 26, 2005
  4. Replies:
    2
    Views:
    359
    kartik
    Aug 16, 2005
  5. Ilya Zakharevich

    Detecting Bourne (or csh) shell from Perl

    Ilya Zakharevich, Mar 3, 2010, in forum: Perl Misc
    Replies:
    8
    Views:
    182
    Ilya Zakharevich
    Mar 10, 2010
Loading...

Share This Page