Perl脚本与另一个程序的STDIN交互(Perl script interacting with another program's STDIN)

编程入门 行业动态 更新时间:2024-10-09 05:18:18
Perl脚本与另一个程序的STDIN交互(Perl script interacting with another program's STDIN)

我有一个Perl脚本,用反引号调用另一个程序,并检查输出是否有某些字符串。 这运行良好。

我遇到的问题是,其他程序在做什么时会失败并等待用户输入。 它要求用户在退出程序之前按两次输入。

如何告诉我的Perl脚本在此程序中按两次输入?

I have a Perl script that calls another program with backticks, and checks the output for certain strings. This is running fine.

The problem I have is when the other program fails on what it is doing and waits for user input. It requires the user to press enter twice before quitting the program.

How do I tell my Perl script to press enter twice on this program?

最满意答案

startup命令从您的脚本接收相同的STDIN和STDERR,只是STDOUT通过管道传送到您的脚本。

您可以在运行命令之前关闭STDIN,并且不会有输入源。 从STDIN读取将导致错误,并且被调用的命令将退出:

close STDIN; my @slines = `$command`;

这也将使控制台无法输入到脚本中。

另一种方法是使用IPC::Open2 ,它允许你的脚本同时控制命令的STDIN和STDOUT:

use IPC::Open2; open2($chld_in, $chld_in, 'some cmd and args'); print $chld_in "\n\n"; close $chld_in; @slines = <$chld_out>; close $chld_out;

该脚本提供命令所需的两个\n输入并读取命令输出。

The started command receives the same STDIN and STDERR from your script, just STDOUT is piped to your script.

You could just close your STDIN before running the command and there will be no input source. Reading from STDIN will cause an error and the called command will exit:

close STDIN; my @slines = `$command`;

This will also void any chance of console input to your script.

Another approach would use IPC::Open2 which allows your script to control STDIN and STDOUT of the command at the same time:

use IPC::Open2; open2($chld_in, $chld_in, 'some cmd and args'); print $chld_in "\n\n"; close $chld_in; @slines = <$chld_out>; close $chld_out;

This script provides the two \n input needed by the command and reads the command output.

更多推荐

本文发布于:2023-08-04 20:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422163.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   程序   Perl   STDIN   program

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!