Python和Bash:如何自动化用户输入?
问题描述:
我下载了python和bash中的一些脚本,提示用户输入命令行。由于我经常运行这些脚本,因此我想自动将输入提供给程序。我不想修改脚本。有没有办法做到这一点,而无需修改原始代码?Python和Bash:如何自动化用户输入?
[更新]感谢EnabrenTane的建议,它似乎工作得很好,直到我得到了一条线,读取password = getpass.getpass('password: ')
。它抱怨以下几点:
File "/usr/lib/python2.4/getpass.py", line 29, in unix_getpass
old = termios.tcgetattr(fd) # a copy to save
termios.error: (25, 'Inappropriate ioctl for device')
任何方式来解决?
答
由于密码要求,我最终使用pexpect,这是一个自动化用户输入的python模块。
答
像这样的庆典:$ ./python_script < input.txt
编辑: 另外,您可以编写脚本采取ARGV
作为文件名从阅读。您可以重新打开STDIN
到文件,而不必更改任何其他行。
答
由于via stdin中的响应:
cat answers | yourscript.py
http://sourceforge.net/projects/expect/ –
为什么你会期望输入被打印两次? – kindall
kindall:因为第一个建议@ http://www.linuxquestions.org/questions/programming-9/send-automatic-input-to-a-script-called-by-another-script-in-bash-programming -164502 / – PJx