相关疑难解决方法(0)

在Perl中读取STDIN上的管道输入后,我可以提示用户输入吗?

我正在尝试编写一个读取管道数据的perl脚本,然后根据该数据提示用户输入.prompt_for_action我正在尝试执行以下脚本:

#!/usr/bin/perl 

my @hosts = ();
while (<>) {
    my $host = $_;
    $host =~ s/\n//; # strip newlines
    push(@hosts, $host);
}

for my $host (@hosts) {
    print "Do you want to do x with $host ? y/n: ";
    chomp(my $answer = <>);
    print "You said `$answer`.\n";
}
Run Code Online (Sandbox Code Playgroud)

但是当我运行它时,没有等待用户输入,它只是吹过而没有等待我输入:

$ echo "test1.example.com
> test2.example.com" | ./prompt_for_action
Do you want to do x with test1.example.com ? y/n: You said ``.
Do you want to do x with test2.example.com ? …
Run Code Online (Sandbox Code Playgroud)

perl command-line stdin

12
推荐指数
1
解决办法
2724
查看次数

标签 统计

command-line ×1

perl ×1

stdin ×1