在命令行中自动输入输入

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

有一个专门针对该案例创建的命令: yes

 $ yes | ./script 

这样做是将./script的输入连接到yes的输出。 因此,当./script请求用户输入时,它将获得yes的输出。 yes的输出yes y循环,然后是换行符。 所以基本上好像用户为./script每个问题输入y

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

 $ yes n | ./script 

请注意,某些工具提供了一个不要求的选项,并始终将yes作为答案。 请参见此处: 绕过“apt-get upgrade”中的yes / no提示符


输入输入的其他方法:

如果您确切知道脚本的预期数量,您可以这样做:

 $ printf 'y\ny\ny\n' | ./script 

换行符( \n )是输入键。

使用printf而不是yes你可以对输入进行更精细的控制:

 $ printf 'yes\nno\nmaybe\n' | ./script 

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

 $ printf 'yyy' | ./script 

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

 $ ./script << EOF y y y EOF 

或者如果你的shell支持这里的字符串 :

 $ ./script <<< "y y y " 

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

 $ ./script < inputfile 

如果命令足够复杂并且上面的方法不再足够,那么你可以使用expect 。

这是一个超级简单的期望脚本的示例:

 spawn ./script expect "are you sure?" send "yes\r" expect "are you really sure?" send "YES!\r" expect eof 

技术挑剔:

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

 $ ./script < echo 'yyyyyyyyyyyyyy' bash: echo: No such file or directory 

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

 $ ./script 'yyyyyyyyyyyyyy' < echo bash: echo: No such file or directory 

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

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

如果您需要更复杂的东西,可以将脚本包装在expect脚本中。 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 

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

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

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

期待在Bash内

使用命令yes

 yes | script 

摘自手册页:

 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'. 

好吧,这可能不是一个非常优雅的解决方案,但如果你在一个单独的文件中编写选项,然后将其作为脚本的输入传递,它也可以工作。 因此,如果您创建一个包含所有选项的新文件(将此文件称为’options.in’),则可以使用./script.sh < options.in轻松运行脚本,并根据需要编辑/创建不同的选项文件。

我正在用Dialog编写一个bash脚本,并且还需要自动执行此操作。 我做到了这一点,它就像一个魅力。

 # -Wy force signaturewipe (if exists) echo "y" | sudo lvcreate -W y -n $lvName -L "$lvSize"G /dev/"$svg" >> $nfsUtilLog 

您可以使用cat ,从文本文件,使用bash将脚本提供给脚本的用户输入,如下所示:

 cat input.txt | bash your_script.sh 

只需将您想要的用户输入放入input.txt文件中,无论您想要什么答案 – y,n,数字,字符串等。