Can I run a bash script in Python and keep any env variables it exports? -
the title pretty says all..
suppose have bash script:
#!/bin/bash # magic here, perhaps fetch wget, , then: if [ "$var1" = "foo" ]; export casevara=1 fi export casevarb=2 # , potentially many other vars...
how can run script python , check env variables set. ideally, i'd "reverse-inherit" them main environment running python.
so can access them with
import os # run bash script somehow print os.environ['casevara']
certainly! requires hacks:
variables = subprocess.popen( ["bash", "-c", "trap 'env' exit; source \"$1\" > /dev/null 2>&1", "_", "yourscript"], shell=false, stdout=subprocess.pipe).communicate()[0]
this run unmodified script , give exported variables in form foo=bar
on different lines.
on supported os (like gnu) can trap 'env -0' exit
\0
separated variables, support multiline values.
Comments
Post a Comment