Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem with your approach is that you are trying to interpret bash scripts. First you just try to interpret the export statement. Then you notice people are using variable expansion. Later people will put conditionals in their files, or process substitutions. In the end you will have a full blown bash script interpreter with a gazillion bugs. Don't do that.</p> <p>Let Bash interpret the file for you and then collect the results.</p> <p>You can do it like this:</p> <pre><code>#! /usr/bin/env python import os import pprint import subprocess command = ['bash', '-c', 'source init_env &amp;&amp; env'] proc = subprocess.Popen(command, stdout = subprocess.PIPE) for line in proc.stdout: (key, _, value) = line.partition("=") os.environ[key] = value proc.communicate() pprint.pprint(dict(os.environ)) </code></pre> <p>Make sure that you handle errors in case bash fails to <code>source init_env</code>, or bash itself fails to execute, or subprocess fails to execute bash, or any other errors.</p> <p>Read the documentation on <a href="http://docs.python.org/library/subprocess.html" rel="noreferrer">subprocess</a> for more details.</p> <p>Note: this will only capture variables set with the <code>export</code> statement, as <code>env</code> only prints exported variables.</p> <p>Enjoy.</p> <p>Note that the <a href="http://docs.python.org/library/os.html#os.environ" rel="noreferrer">Python documentation</a> says that if you want to manipulate the environment you should manipulate <code>os.environ</code> directly instead of using <code>os.putenv()</code>. I consider that a bug, but I digress.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload