How to automatically input using python Popen and return control to command line
<p>I have a question regarding subprocess.Popen .I'm calling a shell script and provide fewinputs. After few inputs ,I want user running the python script to input.</p>

<p>Is it possible to transfer control from Popen process to command line.</p>

<p>I've added sample scripts</p>

<p>sample.sh</p>

<pre><code>echo "hi"
read input_var
echo "hello" $input_var
read input_var1
echo "Whats up" $input_var1
read input_var2
echo "Tell something else" $input_var2
</code></pre>

<p>test.py</p>

<pre><code>import os
from subprocess import Popen, PIPE

p=Popen([path to sample.sh],stdin=PIPE)
#sample.sh prints asks for input
p.stdin.write("a\n")
#Prompts for input
p.stdin.write("nothing much\n")
#After the above statement ,User should be able to prompt input 
#Is it possible to transfer control from Popen to command line  
</code></pre>

<p>Output of the program python test.py </p>

<pre><code>hi
hello a
Whats up b
Tell something else
</code></pre>

<p>Please suggest if any alternate methods available to solve this</p>

<p>As soon as your last <code>write</code> is executed in your python script, it will exit, and the child shell script will be terminated with it. You request seems to indicate that you would want your child shell script to keep running and keep getting input from the user. If that's the case, then <code>subprocess</code> might not be the right choice, at least not in this way. On the other hand, if having the python wrapper still running and feeding the input to the shell script is enough, then you can look at something like this:</p>

<pre><code>import os
from subprocess import Popen, PIPE

p=Popen(["./sample.sh"],stdin=PIPE)
#sample.sh prints asks for input
p.stdin.write("a\n")
#Prompts for input
p.stdin.write("nothing much\n")

# read a line from stdin of the python process
# and feed it into the subprocess stdin.
# repeat or loop as needed
line = raw_input()
p.stdin.write(line+'\n')
# p.stdin.flush()   # maybe not needed
</code></pre>

<p>As many might cringe at this, take it as a starting point. As others have pointed out, stdin/stdout interaction can be challenging with subprocesses, so keep researching.</p>

python shell command-line