1

I have an installer of an application. It has .sh and .bat for *nix/windows os. In the scripts it will do something and hang there, waiting some other things to be done, then need press Enter in that command window, the scripts will continue.

I want to do all the stuff with python. i know subprocess.open can invoke script, but im not sure how to do the "press Enter" thing..

Any input will be appraciated..

udondan
  • 57,263
  • 20
  • 190
  • 175
Luke
  • 13
  • 1
  • 3

2 Answers2

3

One way is to use is to use the raw_input method!

# Code
print "Please press ENTER to continue!"
raw_input()
# more code
Aneesh Dogra
  • 740
  • 5
  • 30
  • thanks for your reply Aneesh. but what i was tring to do is an automation-install tool, which will do all the things automatically. So is there anyway which does not involve people operation? – Luke Mar 26 '13 at 14:14
2

You need to supply a newline on the installer's stdin.

One way:

subprocess.Popen('sh abc.sh < a_file_that_you_prepared.txt',
                 shell=True,
                 stdout=subprocess.PIPE)

Another, roughly equivalent to the first:

input_file = open('a_file_that_you_prepared.txt', 'r')
subprocess.Popen('sh abc.sh',
                 shell=True,
                 stdout=subprocess.PIPE,
                 stdin=input_file)
input_file.close()

Another way -- might work, might not:

subprocess.Popen('sh abc.sh < /dev/null',
                 shell=True,
                 stdout=subprocess.PIPE)

Third way -- can easily cause deadlock between your program and installer:

x = subprocess.Popen('sh abc.sh',
                 shell=True,
                 stdout=subprocess.PIPE,
                 stdin=subprocess.PIPE)

...

x.stdin.write('\n')
Robᵩ
  • 163,533
  • 20
  • 239
  • 308