Get Environment Variables From A .sh Script And Use Them In My Python Script
Solution 1:
I'm not sure what you mean by bat script, but in any case, you have to deal with two problems:
(1) While you, basically, can name the environment variables in any way you like, you should use UPPER CASE letters only, to avoid portability issues. That is, your environment variable should be named AENV.
(2) As you correctly recognized, you can not "transport" an environment variable from one process to another one, unless the other process is a child process of the process which sets the environment variable. Hence you need to store the value somewhere else. The following three possibilities are a common way to do it:
The process which defines the value of the environment variables could output name and value to stdout. The calling script catches the output and sets the variables in its own environment. When the Python script is called, the variable will be there.
The generating script writes the value of the environment variable to a file and the Python program fetches it from the file.
"Continuation passing style": The master script does not call A.sh and the Python program, but only A.sh, and passes the invocation line of the Python process to A.sh (either as a parameter or via the environment). A.sh sets the environment, invokes the Python program, and passes the exit code of the Python program back to the caller.
Solution 2:
Change export AEnv==1234
into export AEnv=1234
.
The bat-file
will never know the environ settings from A.sh.
Try to call your Python from B.sh
:
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ROOT_DIR="$SCRIPT_DIR/.."
. ${SCRIPT_DIR}/A.sh
echo"After calling A.sh: AEnv=${AEnv}"
python C/Python27/lib/os.py
Post a Comment for "Get Environment Variables From A .sh Script And Use Them In My Python Script"