[Tutor] Fwd: RE: How to source a shell script through python to set the environment variables.

Aneeque Khan aneeque.khan at ericsson.com
Thu Feb 25 23:49:41 EST 2016


Hi guys, any suggestions on this.

-----Original Message-----
From: Tutor [mailto:tutor-bounces+aneeque.khan=ericsson.com at python.org] On Behalf Of Alan Gauld
Sent: Tuesday, February 23, 2016 5:39 PM
To: tutor
Subject: [Tutor] Fwd: RE: How to source a shell script through python to set the environment variables.

Forwarding to list.
Please always use ReplyAll (or replyList) when responding to tutor.


-------- Forwarded Message --------
Subject: 	RE: [Tutor] How to source a shell script through python to set
the environment variables.
Date: 	Tue, 23 Feb 2016 11:14:02 +0000
From: 	Aneeque Khan <aneeque.khan at ericsson.com>
To: 	Alan Gauld <alan.gauld at btinternet.com>



Please see my comments with [AK]

-----Original Message-----
From: Tutor [mailto:tutor-bounces+aneeque.khan=ericsson.com at python.org] On Behalf Of Alan Gauld
Sent: Tuesday, February 23, 2016 4:16 PM
To: tutor at python.org
Subject: Re: [Tutor] How to source a shell script through python to set the environment variables.

On 23/02/16 07:29, Aneeque Khan wrote:

> env_test.sh looks like this :-
> #!/bin/bash
> export OS_AK="Aneeque"
> export OS_KHAN="KHAN"
> 
> echo "Please enter your Stack Password: "
> read -sr OS_PWD_INPUT
> export OS_PWD=$OS_PWD_INPUT
> 
> Now I want to execute the above script through python file named 
> "ak_test.py", to set these variables and access them further in the code.

Can I first ask why you are trying to do it via a shell script rather than using Python directly? Is this part of a bigger workflow?

[AK] :- this is the part of bigger workflow, where I get the shell script as a output of some processing.
 
The problem is that virtually any method of running a shell script will involve starting a separate process and setting the variables in that process' environment.

So far as I can tell the only way to do what you want would be to run your shell script first then launch your python script from the shell script. That would mean breaking your current python script in two, and if you do a lot of work prior to launching the shell, that's probably not practical.

[AK] :- I can`t set the variables from outside the python script as our workflow requires to process no. of such shell scripts. We have to process each shell script to set the environment and work on further.
	You can assume that overall workflow have large no. of users each having its own values for certain environment variables. While performing any task for that particular user we require to set the environment first.


> def update_env(script_path):
>    if "--child" in sys.argv: # executed in the child environment
>       pprint(dict(os.environ))
>    else:
>       python, script = quote(sys.executable), quote(sys.argv[0])
>       os.execl("/bin/bash", "/bin/bash", "-c", "source %s; %s %s 
> --child" % (script_path, python, script))
> 
> with this approach some variables set while others not.

That surprises me since calling execl() overwrites the calling process with the called process, so I would not expect you to see anything in your calling script.

How are you testing the results?
Are you checking os.environ?
Or are you using os.getenv()?

[AK] :- I am checking like this :- 
	 os.getenv("OS_AK")
	os.getenv("OS_PWD")
and getting None as the result.

> Another approach used :-
> def update_env2(script):
>     #pipe1 = subprocess.Popen(". %s; env -0" % script, stdout=subprocess.PIPE, shell=True, executable="/bin/bash")
>     pipe1 = subprocess.Popen(". %s; env" % script, 
> stdout=subprocess.PIPE, shell=True, executable="/bin/bash")
> 
>     output = pipe1.communicate()[0]
>     #env = dict((line.split("=", 1) for line in output.splitlines()))
>     env = dict((line.split("=", 1) for line in output.split('\0')))
> 
>     os.environ.update(env)

While this could work you are not really reading the environment variables you are simply reading the stdout. You could achieve the same by parsing the script as a text file.

So can you explain why you need to load these values from a shell script rather than setting os.environ directly?
Maybe we can solve the bigger issue.

[AK] :- In workflow neither variable names nor their values are known in advance. We only came to know about these at run time through the generated shell scripts.
I have recently started with python, can you direct me how we can achieve this by parsing the shell as a text file.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list