By Dr. Debasish Jana


2020-03-25 19:55:16 8 Comments

I have a legacy python code that reads from a shell script for variable values like in a properties file. For example, say, I have a shell program x.sh for variable declaration as:

Y_HOME=/utils
Y_SPCL=$Y_HOME/spcl
UTIL1=$Y_SPCL/u1
Y_LIB=$Y_HOME/lib

Now, from within python program abc.py, I read x.sh file line by line and use line.split("=")[1] to get the value of the variable, say, UTIL1 as $Y_SPCL/u1 in non-expanded form and not in expanded form as /utils/spcl/u1. Can I have sone mechanism in python to have vafiable expandion like in a shell program execution. I think, since I am using x.sh not as a shell program, rather as a configuration file like properties, there should be all variables in expanded form to let the python program run properly, such as:

Y_HOME=/utils
Y_SPCL=/utils/spcl
UTIL1=/utils/spcl/u1
Y_LIB=/utils/lib

This will have no change on the legacy python part of code and changing the configuration file as an external properties data. Please pass your opinions.

2 comments

@DarK_FirefoX 2020-03-25 20:50:09

Assuming variables must be declared before use and they form a correct path when expanded, you could do something like this.

There is the file fileName that contains the variables:

Y_HOME=/utils
Y_SPCL=$Y_HOME/spcl
UTIL1=$Y_SPCL/u1
Y_LIB=$Y_HOME/lib

So, For each "variable", you search for it in the next variables "values" and replace it for the proper "value". You could have a .py like this:

variables = []
with open("fileName", 'r') as f:
    while True:
        line = f.readline()[:-1] # Get the line minus '\n'
        if not line:
            break
        variables.append(line.split('=')) 

    for i in range(len(variables)):
        current =  "$" + variables[i][0]
        for j in range(len(variables)):
            replaced = None
            while replaced != variables[j][1]:
                # We replace until no further replaces happen
                replaced = variables[j][1].replace(current, variables[i][1])
                variables[j][1] = replaced

for var in variables:
    print(var[0] + "=" + var[1])

Output:

Y_HOME=/utils
Y_SPCL=/utils/spcl
UTIL1=/utils/spcl/u1
Y_LIB=/utils/li

@Johnny Wezel 2020-03-25 20:12:53

There is a package dotenv that can do that.

pip install --user python-dotenv

Then, in Python:

import dotenv, os
dotenv.load_env("x.sh")
print(os.environ["Y_LIB"])

Important: Make sure your variable substitutions read like ${VAR}. So your x.sh would look like this:

Y_HOME=/utils
Y_SPCL=${Y_HOME}/spcl
UTIL1=${Y_SPCL}/u1
Y_LIB=${Y_HOME}/lib

Related Questions

Sponsored Content

35 Answered Questions

[SOLVED] How can I reverse a list in Python?

  • 2010-10-15 06:59:39
  • Leo.peis
  • 1440340 View
  • 1012 Score
  • 35 Answer
  • Tags:   python list

28 Answered Questions

[SOLVED] How can you profile a Python script?

15 Answered Questions

[SOLVED] "Large data" work flows using pandas

30 Answered Questions

[SOLVED] Why doesn't "cd" work in a shell script?

  • 2008-11-01 02:07:08
  • ashokgelal
  • 617934 View
  • 756 Score
  • 30 Answer
  • Tags:   linux shell

21 Answered Questions

[SOLVED] How can I declare and use Boolean variables in a shell script?

43 Answered Questions

[SOLVED] How can I represent an 'Enum' in Python?

19 Answered Questions

[SOLVED] How can I use threading in Python?

18 Answered Questions

[SOLVED] Are static class variables possible in Python?

2 Answered Questions

[SOLVED] Shell variable expansion in git config

  • 2012-06-29 12:44:02
  • SvenK
  • 12502 View
  • 54 Score
  • 2 Answer
  • Tags:   git shell

Sponsored Content