简体   繁体   English

在perl脚本中执行命令时,perl可以响应命令中提出的STDOUT请求吗?

[英]When executing a command in a perl script can perl respond to a STDOUT request posed in the command?

I'm executing a command inside a perl script and when that command completes, a question is sent to STDOUT requesting a Y or N answer to a question. 我正在perl脚本中执行命令,当该命令完成时,将向STDOUT发送一个问题,要求对该问题回答Y或N。 If no answer is given (ie I just end the script) then we have a hung process in the shell waiting for an answer. 如果没有给出答案(即我只是结束脚本),那么外壳中就会有一个挂起的进程在等待答案。 How can I supply the desired Y answer? 如何提供所需的Y答案?

perl v5.8.4 solaris 10 perl v5.8.4 solaris 10

  • Simplest : 最简单的

    Use shell's ability to redirect "Y" into command's STDIN: 使用shell的能力将“ Y”重定向到命令的STDIN:

     `echo "Y" | your_command_expecting_Y`; 

    or (slightly worse but more flexible). 或(稍差一些但更灵活)。

     `your_command_expecting_Y < /my/file/containing/one/line/with_Y_in_it.txt`; 
  • More complicated but infinitely more flexible and Perl native: 更复杂但无限灵活和Perl本机:

    Use Expect module 使用Expect模块

     use Expect; # create an Expect object by spawning another process my $exp = Expect->spawn($command, @params); $exp->send("Y\\n"); 

Assuming you always want to answer 'Y' and the command will only prompt once : 假设您总是想回答“ Y”,并且该命令仅提示一次

system("echo Y | your_command_here");

If the command will prompt more than once and you always want to answer 'Y': 如果该命令将提示多次,而您总是要回答“ Y”:

system("yes Y | your_command_here");

Otherwise, Expect is probably your best bet as the others have suggestes. 否则, Expect可能是其他人建议的最好选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM