按回车作为命令输入 [英] Press enter as command input

查看:25
本文介绍了按回车作为命令输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序的安装程序.它具有用于 *nix/windows 操作系统的 .sh 和 .bat.在脚本中它会做一些事情并挂在那里,等待其他一些事情完成,然后需要在该命令窗口中按 Enter,脚本将继续.

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.

我想用python做所有的事情.我知道 subprocess.open 可以调用脚本,但我不确定如何执行按 Enter"操作.

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..

推荐答案

您需要在安装程序的标准输入中提供换行符.

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

一种方式:

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')

这篇关于按回车作为命令输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆