2

I'm pretty new to Python and trying to find an approach for setting up several shell env variables and then executing a shell script.

Is the subprocess module capable of sending several shell commands and capturing the output?

This isn't clear to me, since each spawn will result in a new shell thread that will not have the previous env variable.

Also, would it be appropriate to use popen or check_output?

Essentially what I'd like to do is:

$ setenv a b
$ setenv c d
$ setenv e f
$ check_status.sh > log.log
$ grep "pattern" log.log

Where check_status.sh must have the above env variables defined to run properly, and it also must be shell script (i.e. I can't translate check_status.sh to python).

I'd appreciate you comments and input.

Simply_me
  • 2,840
  • 4
  • 19
  • 27
  • @favoretti haven't tried, just thinking of an approach. Also not looking for a code solution, just your thought on the problem statement. – Simply_me Jul 22 '14 at 06:36
  • @favoretti although ["Interacting with Another Command"](http://pymotw.com/2/subprocess/) section seems interesting. – Simply_me Jul 22 '14 at 06:38
  • Have you seen this one? http://stackoverflow.com/questions/5971312/how-to-set-environment-variables-in-python? Note that it says that child processes auto-inherit set values, so short answer is yes, it's possible. :) – favoretti Jul 22 '14 at 06:40
  • And here http://stackoverflow.com/questions/20964515/calling-a-subprocess-in-python-with-environmental-variables is a code example. Ignore the `su` part, you don't need it, his problem is that `su` resets the environment. – favoretti Jul 22 '14 at 06:41

1 Answers1

0

subprocess will create one thread everytime. You can use popen to execute shell command. Use semicolons as separators. Such as os.popen('setenv a b;set env c d')

Hope it helps.

Stephen Lin
  • 4,852
  • 1
  • 13
  • 26