Last active
December 18, 2015 11:59
-
-
Save chiangbing/5779195 to your computer and use it in GitHub Desktop.
Read variables in bash script by sourcing the file and echoing the variable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_var(srcfile, *variables): | |
"""Get variables' values from bash shell source file. | |
Return a dictionary that maps from variable name to its value. | |
""" | |
cmd = ". " + srcfile + ";" | |
for var in variables: | |
cmd += "echo ${" + var + "};" | |
proc = subprocess.Popen([cmd], stdout=subprocess.PIPE, shell=True) | |
output = proc.communicate()[0] | |
proc.stdout.close() | |
if output is None: | |
return {} | |
else: | |
values = output.split("\n") | |
return dict(zip(variables, values)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment