在命令行中自动输入输入

New*_*Mrd 202 command-line input

我正在运行一个脚本,它要求在每个操作中输入“y”,我正在寻找一种解决方案,例如$ ./script < echo 'yyyyyyyyyyyyyy'一次性传递我的所有输入。

les*_*ana 345

有一个专门为这种情况创建的命令: yes

$ yes | ./script
Run Code Online (Sandbox Code Playgroud)

这样做是将 的输出连接yes到 的输入./script。因此,当./script要求用户输入时,它将获得yes. 的输出yes是无穷无尽的,y后面跟着一个换行符。所以基本上是如果用户输入y的每一个问题./script

如果你想说 no ( n) 而不是 yes ( y) 你可以这样做:

$ yes n | ./script
Run Code Online (Sandbox Code Playgroud)

请注意,某些工具可以选择始终假设yes为答案。例如,请参见此处:绕过“apt-get upgrade”中的是/否提示


输入输入的其他方法:

如果您确切地知道y您的脚本期望多少,您可以这样做:

$ printf 'y\ny\ny\n' | ./script
Run Code Online (Sandbox Code Playgroud)

换行符 ( \n) 是回车键。

使用printf而不是yes你有更细粒度的输入控制:

$ printf 'yes\nno\nmaybe\n' | ./script
Run Code Online (Sandbox Code Playgroud)

请注意,在极少数情况下,该命令不需要用户在字符后按 Enter。在这种情况下,请忽略换行符:

$ printf 'yyy' | ./script
Run Code Online (Sandbox Code Playgroud)

为了完整起见,您还可以使用此处的文档

$ ./script << EOF
y
y
y
EOF
Run Code Online (Sandbox Code Playgroud)

或者,如果您的 shell 支持此处的字符串

$ ./script <<< "y
y
y
"
Run Code Online (Sandbox Code Playgroud)

或者您可以创建一个每行一个输入的文件:

$ ./script < inputfile
Run Code Online (Sandbox Code Playgroud)

如果命令足够复杂并且上述方法不再足够,那么您可以使用expect

这是一个超级简单的expect脚本的例子:

spawn ./script
expect "are you sure?"
send "yes\r"
expect "are you really sure?"
send "YES!\r"
expect eof
Run Code Online (Sandbox Code Playgroud)

技术挑剔:

您在问题中给出的假设命令调用不起作用:

$ ./script < echo 'yyyyyyyyyyyyyy'
bash: echo: No such file or directory
Run Code Online (Sandbox Code Playgroud)

这是因为 shell 语法允许在命令行中的任何位置使用重定向运算符。就 shell 而言,您假设的命令行与此行相同:

$ ./script 'yyyyyyyyyyyyyy' < echo
bash: echo: No such file or directory
Run Code Online (Sandbox Code Playgroud)

这意味着./script将使用参数调用'yyyyyyyyyyyyyy'并且标准输入将从名为echo. 并且 bash 抱怨,因为该文件不存在。

  • 我得到一个“无法在非 tty 输入上启用 tty 模式”。你知道一个解决方法吗? (2认同)

小智 13

使用命令yes

yes | script
Run Code Online (Sandbox Code Playgroud)

摘自手册页:

NAME
       yes - output a string repeatedly until killed

SYNOPSIS
       yes [STRING]...
       yes OPTION

DESCRIPTION
       Repeatedly output a line with all specified STRING(s), or 'y'.
Run Code Online (Sandbox Code Playgroud)


Oli*_*Oli 9

有些东西(apt-get例如)接受特殊标志以静默模式运行(并接受默认值)。在apt-get's 的情况下,您只需向它传递一个-y标志。不过,这完全取决于您的脚本。

如果您需要更复杂的东西,您可以将您的脚本包装在一个期望脚本中。expect 允许您读取输出并发送输入,因此您可以执行其他脚本不允许的非常复杂的事情。这是其维基百科页面的示例之一

# Assume $remote_server, $my_user_id, $my_password, and $my_command were read in earlier
# in the script.
# Open a telnet session to a remote server, and wait for a username prompt.
spawn telnet $remote_server
expect "username:"
# Send the username, and then wait for a password prompt.
send "$my_user_id\r"
expect "password:"
# Send the password, and then wait for a shell prompt.
send "$my_password\r"
expect "%"
# Send the prebuilt command, and then wait for another shell prompt.
send "$my_command\r"
expect "%"
# Capture the results of the command into a variable. This can be displayed, or written to disk.
set results $expect_out(buffer)
# Exit the telnet session, and wait for a special end-of-file character.
send "exit\r"
expect eof
Run Code Online (Sandbox Code Playgroud)


Tar*_*run 7

在 shell 脚本中,您还可以使用以下 spawn、expect 和 send 技巧

spawn script.sh
expect "Are you sure you want to continue connecting (yes/no)?"
send "yes"
Run Code Online (Sandbox Code Playgroud)

但是,在上面的场景中,您必须给出执行脚本时期望得到的短语,以获取更多示例,请转到以下链接

在 Bash 中期待