2

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.

Is it possible to transfer control from Popen process to command line.

I've added sample scripts

sample.sh

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

test.py

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  

Output of the program python test.py

hi
hello a
Whats up b
Tell something else

Please suggest if any alternate methods available to solve this

Abhishek L
  • 81
  • 2
  • 10
  • 1
    why doesn't it work? can you explain the problem a bit more? – benten Sep 15 '16 at 01:37
  • In the above program ,After I run test.py. input_var2 was expected to be entered by user.But it didnt happen.Output just had "Tell something else" .Am I missing something? – Abhishek L Sep 15 '16 at 01:42
  • @John1024 tried the above suggestion.Same result.Thanks. – Abhishek L Sep 15 '16 at 01:50
  • Thanks @John1024 As you pointed out ,Initially `p=Popen([path to sample.sh],stdin=PIPE)` was given. And later I tried `p=Popen(["./sample.sh"],stdin=PIPE)`.I didn't see any error messages.I pass first two variables from python to shell script and expect third one to be entered from user.However code doesn't ask for third variable .I'm not able to attach screenshot. I'm running this program from putty ssh session. – Abhishek L Sep 15 '16 at 12:24
  • @John1024 Yes you are right if `sample.sh` given without proper path.I used to get `NameError` error.I have corrected the question.After correcting `test.py` it or after running it inside actual directory within quotes `./sample.sh` ,I'm facing above problem.Please guide if anyone has faced this issue earlier. – Abhishek L Sep 15 '16 at 14:15
  • @Abhishek If my answer below covers your case please accept the answer – sal Sep 15 '16 at 22:29

1 Answers1

4

As soon as your last write 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 subprocess 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:

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

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.

sal
  • 3,515
  • 1
  • 10
  • 21
  • (1) This appears to me to solve the OP's issue: +1. (2) One probably should add p.wait() to the end of the code to avoid, if python were to terminate _before_ the script, creating a zombie. (3) Also, for more generality, `raw_input...` could, as you know, be put in a loop. – John1024 Sep 15 '16 at 20:14
  • Thank you @sal. It worked.I'll read about stdin/stdout interaction subprocess. – Abhishek L Sep 16 '16 at 04:08