bash 在 shell 脚本中模拟“ENTER”键按下

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33600584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:53:40  来源:igfitidea点击:

Simulate "ENTER" key press in shell script

bashshellunixkeyboardautomation

提问by Zack

I want to automate the run of the ./configurecommand. When I normally run it, every time I have to press enter. I want to run this command from a shell script and I don't want it to wait for a user to press enter at each prompt for path. How do I achieve this ? I am using Ubuntu machine with bash shell. Thanks.

我想自动化./configure命令的运行。当我正常运行它时,每次我都必须按回车键。我想从 shell 脚本运行这个命令,我不希望它等待用户在每个提示输入路径时按 Enter。我如何实现这一目标?我正在使用带有 bash shell 的 Ubuntu 机器。谢谢。

mdt-inference@ubuntu:~/MDT/mdst-libreoffice$ ./configure 
Path to Office installation? [/usr/lib/libreoffice] 
Path to Office SDK installation? [/usr/lib/libreoffice/sdk]

Note : I tried the below link but it does not work. How to simulate two consecutive ENTER key presses for a command in a bash script?

注意:我尝试了下面的链接,但它不起作用。 如何在 bash 脚本中为一个命令模拟两次连续的 ENTER 键按下?

I have used "yes" command already but when I run ./configure there are multiple prompts which are more than two. When I use "yes", it just supplies argument to the first prompt only

我已经使用了“是”命令,但是当我运行 ./configure 时,会出现多个超过两个的提示。当我使用时"yes",它只为第一个提示提供参数

回答by balabhi

yescommand can be used here (with just 2 enter key)

yes可以在此处使用命令(只需 2 个回车键)

yes " " | head -2 | ./configure  

In general, this should work.

一般来说,这应该有效。

yes | ./configure

回答by Bill Agee

You might check out the cram Python package:

您可以查看补习 Python 包:

https://pypi.python.org/pypi/cram

https://pypi.python.org/pypi/cram

It's designed for straightforward automation of command-line apps, and I've been pleasantly surprised by how well it's worked for my needs so far.

它专为命令行应用程序的直接自动化而设计,到目前为止,它对我的​​需求如此有效,令我感到惊喜。