简体   繁体   中英

Python 2.7 keep env variables from a subprocess

I'm calling a bash script which is exporting a few variables, i found a way to get those variables and it's working, once i'm trying to add to args to my bash script it's failing. Here is part of my python script:

bash_script = "./testBash.sh"
script_execution = Popen(["bash", "-c", "trap 'env' exit; source \"$1\"  > /dev/null 2>&1",

                                  "_", bash_script], shell=False, stdout=PIPE)
err_code = script_execution.wait()
variables = script_execution.communicate()[0]

This is my sample Bash script:

export var1="test1"

export var2=$var1/test2

echo "this is firsr var: var1=$var1"
echo "this is the second var: var2=$var2"

Once i'm changing the bash_script = "./testBash.sh" to bash_script = "./testBash.sh test test" I'm not getting back the exported variables from the bash script into the variables variable in the Python script. The provided above is a sample, and of course my real scripts are much more bigger.

If you change bash_script = "./testBash.sh" to bash_script = "./testBash.sh test test" then the name of the bash_script changes to "./testBash.sh test test" . The 'test test' is not interpreted as separate arguments.

Instead, add the extra arguments to the list being passed to Popen :

bash_script = "./testBash.sh"
script_execution = Popen(
["bash", "-c", "trap 'env' exit; source \"$1\"  > /dev/null 2>&1",
 "_", bash_script, 'test', 'test'], shell=False, stdout=PIPE)

Then err_code will be 0 (indicating success), instead of 1. It's not clear from your posted code however what you want to happen. The extra test arguments are ignored.

The extra argument are received by the bash script, however. If instead you put

export var1="$2"

in testBash.sh , then the variables (in the Python script) would contain

var1=test

You might also find it more convenient to use

import subprocess
import os

def source(script, update=True):
    """
    http://pythonwise.blogspot.fr/2010/04/sourcing-shell-script.html (Miki Tebeka)
    http://stackoverflow.com/a/20669683/190597 (unutbu)
    """
    proc = subprocess.Popen(
        ". %s; env -0" % script, stdout=subprocess.PIPE, shell=True)
    output = proc.communicate()[0]
    env = dict((line.split("=", 1) for line in output.split('\x00') if line))
    if update:
        os.environ.update(env)
    return env

bash_script = "./testBash.sh"
variables = source(bash_script)
print(variables)

which yields the environment variables

{ 'var1': 'test1', 'var2': 'test1/test2', ... }

in a dict.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM