我正在perl脚本中执行命令,当该命令完成时,会向STDOUT发送一个问题,要求Y或N回答问题。如果没有给出答案(即我只是结束脚本),那么我们在shell中有一个等待答案的挂起进程。我如何提供所需的Y答案?
perl v5.8.4 solaris 10
答案 0 :(得分:8)
最简单:
使用shell将“Y”重定向到命令的STDIN:
`echo "Y" | your_command_expecting_Y`;
或(稍差但更灵活)。
`your_command_expecting_Y < /my/file/containing/one/line/with_Y_in_it.txt`;
更复杂但更灵活,Perl原生:
使用Expect
模块
use Expect;
# create an Expect object by spawning another process
my $exp = Expect->spawn($command, @params);
$exp->send("Y\n");
答案 1 :(得分:3)
答案 2 :(得分:3)
假设你总是想要回答'Y'并且命令只会提示一次:
system("echo Y | your_command_here");
如果该命令会多次提示并且总是想要回答'Y':
system("yes Y | your_command_here");
否则,Expect
可能是你最好的选择,因为其他人有建议。