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.

4
  • @favoretti haven't tried, just thinking of an approach. Also not looking for a code solution, just your thought on the problem statement. Commented Jul 22, 2014 at 6:36
  • @favoretti although "Interacting with Another Command" section seems interesting. Commented Jul 22, 2014 at 6:38
  • Have you seen this one? stackoverflow.com/questions/5971312/…? Note that it says that child processes auto-inherit set values, so short answer is yes, it's possible. :) Commented Jul 22, 2014 at 6:40
  • And here stackoverflow.com/questions/20964515/… is a code example. Ignore the su part, you don't need it, his problem is that su resets the environment. Commented Jul 22, 2014 at 6:41

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.