在bash脚本中模拟输入按键

时间:2022-12-09 20:53:54

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脚本,它运行一些命令。其中一个命令在运行时需要用户输入。我。它会问用户“你想说什么?”,我只想向它发送一个enter keypress,以便脚本将完全自动化。

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.

在运行时,我不需要等待输入或任何东西,它只需要发送按键和输入缓冲区就可以处理剩下的部分。

5 个解决方案

#1


84  

echo -ne '\n' | <yourfinecommandhere>

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

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

echo | <yourfinecommandhere>

#2


27  

You might find the yes command useful.

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

See man yes

看到人是的

#3


16  

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 expect for further information.

检查:期待进一步的信息。

#4


13  

You can just use yes.

你可以用yes。

# yes "" | someCommand

#5


9  

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

你可以利用expect (man expect附带示例)。

#1


84  

echo -ne '\n' | <yourfinecommandhere>

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

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

echo | <yourfinecommandhere>

#2


27  

You might find the yes command useful.

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

See man yes

看到人是的

#3


16  

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 expect for further information.

检查:期待进一步的信息。

#4


13  

You can just use yes.

你可以用yes。

# yes "" | someCommand

#5


9  

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

你可以利用expect (man expect附带示例)。