Linux 在 bash 脚本中模拟 ENTER 按键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6264596/
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-08-04 01:02:49  来源:igfitidea点击:

Simulating ENTER keypress in bash script

linuxbashubuntushell

提问by tobbr

I've created a really simple bash script that runs a few commands. one of these commands needs user input during runtime. i.e it asks the user "do you want to blah blah blah?", I want to simply send an enter keypress to this so that the script will be completely automated.

我创建了一个非常简单的 bash 脚本来运行一些命令。这些命令之一在运行时需要用户输入。即它询问用户“你想等等等等吗?”,我想简单地向这个发送一个输入按键,这样脚本将完全自动化。

I won't have to wait for the input or anything during runtime, its enough to just send the keypress and the input buffer will handle the rest.

我不必在运行时等待输入或任何东西,只需发送按键就足够了,输入缓冲区将处理其余部分。

采纳答案by Tilman Vogel

echo -ne '\n' | <yourfinecommandhere>

or taking advantage of the implicit newline that echo generates (thanks Marcin)

或利用 echo 生成的隐式换行符(感谢 Marcin)

echo | <yourfinecommandhere>

Now we can simply use the --skoption:

现在我们可以简单地使用该--sk选项:

--sk, --skip-keypressDon't wait for a keypress after each test

--sk,--skip-keypress不要在每次测试后等待按键

i.e. sudo rkhunter --sk --checkall

IE sudo rkhunter --sk --checkall

回答by barti_ddu

You could make use of expect(man expectcomes with examples).

您可以使用expectman expect带有示例)。

回答by pavium

You might find the yescommand useful.

您可能会发现该yes命令很有用。

See man yes

man yes

回答by kenorb

Here is sample usage using expect:

这是使用的示例用法expect

#!/usr/bin/expect
set timeout 360
spawn my_command # Replace with your command.
expect "Do you want to continue?" { send "\r" }

Check: man expectfor further information.

检查:man expect以获取更多信息。

回答by David Hamner

You can just use yes.

你可以只使用yes

# yes "" | someCommand