Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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.

share|improve this question
    
What have you tried so far? – favoretti Jul 22 '14 at 6:34
    
@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 6:36
    
@favoretti although "Interacting with Another Command" section seems interesting. – Simply_me Jul 22 '14 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. :) – favoretti Jul 22 '14 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. – favoretti Jul 22 '14 at 6:41

1 Answer 1

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.

share|improve this answer
    
thank you for the answer, i'll give it a try and let you know tomorrow. – Simply_me Jul 27 '14 at 0:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.