I have a little test.sh script that I run from the command line ./test.sh and it will ask me for input to 2 questions that I enter, then it loops till I ctrl-c out of it.

I want to write a little python script that can run my test.sh, and enter the inputs to the script questions from variables, var1 and var2 that I set in python.

I also want to have another variable, var3, that loops for x long and runs my test.sh, and ends it then relaunches it every x minutes based on the value of var3

I wrote this code but it doesn't seem to pass the commands into my test.sh after it launches:

import os
os.system('./test.sh arg1')
time.sleep(10)
sh('input answer 1')
time.sleep(5)
sh('input answer 2')
share|improve this question
3  
Read docs.python.org/3/library/subprocess.html notably about popen – Basile Starynkevitch 1 hour ago
1  
What does "then it loops" mean: it asks you 2 questions again or new input is not expected after the initial 2 questions? What does "loops for x long" mean: for x seconds or x times? – J.F. Sebastian 1 hour ago
1  
Is there some reason you don't want to do this in Python? – Tyler Crompton 51 mins ago
feedback

2 Answers

Using pexpect:

import pexpect  # $ pip install pexpect

while var3:  # start ./test.sh again
    output = pexpect.run('./test.sh arg1', 
        timeout=x, 
        events={r'(?i)question 1': var1,
                r'(?i)question 2': var2, 
                pexpect.TIMEOUT: lambda _: True})  # exit after x seconds

Note: subprocess-based solution might be more complex due to buffering issues and it might require additionally pty, select modules, example.

share|improve this answer
feedback
import subprocess
p = subprocess.Popen('./test.sh arg1', stdin=subprocess.PIPE, stdout=subprocess.PIPE)

time.sleep(10)
p.stdin.write('input answer 1') #Probably adding \n is necessary
time.sleep(5)
p.stdin.write('input answer 2') #Probably adding \n is necessary

print p.stdout.readline() # To print one-line output of the shell script
share|improve this answer
I think you mean p.stdout.readline() and p.stdin.write() ? – jdi 45 mins ago
There is no popen. It should be Popen. Also, see Q: Why not just use a pipe (popen())?. In addition your code might dead-lock if ./test.sh generates enough output (see appropriate warning in subprocess docs). – J.F. Sebastian 36 mins ago
@jdi, Thank you very much, these are errors of typing answer in Stackoverflow's editor :) – MostafaR 33 mins ago
@J.F.Sebastian Thank you very much, yes It's not a perfect solution, but I think sometimes we need easy and quick solutions. – MostafaR 32 mins ago
p.stdout.readline() will block forever if ./tesh.sh uses block-buffering for its output (default if stdout is a pipe). The reasons are explained in the link I've provided. – J.F. Sebastian 16 mins ago
feedback

Your Answer

 
or
required, but never shown
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.