如何在 Perl 中将参数从文本文件传递给 ARGV
我有一个参数列表要使用 ARGV
传递到 Perl 脚本中。第四个参数是服务器的名称,但我想向它传递一个文本文件,其中包含名为 servers.txt
的服务器列表。如何传递它并将其用作 ARGV
的参数?
示例文件servers.txt
:
server1
server2
server3
工作代码:
# usage example: ./test.pl Jul 05 2010 <server> <logfile>
# $ARGV[0]=Jul
# $ARGV[1]=05
# $ARGV[2]=2010
# $ARGV[3]=server
# $ARGV[4]=/pathoffile
use strict;
use warnings;
my($mon,$day,$year,$server,$file) = @ARGV;
open(my $fh,"ssh $server cat $file |") or die "can't open log $server:$file: $!\n";
while (my $line = <$fh>) {
if ($line =~ /.* $mon $day \d{2}:\d{2}:\d{2} $year:.*(ERROR:|backup-date=|backup-size=|backup-time=|backup-status)/) {
print $line;
}
}
I have a list of arguments to pass into a Perl script, using ARGV
. The 4th argument, is a server's name, but I want to pass it a text file with a list of servers called servers.txt
. How do I pass that in and use it as an argument to ARGV
?
Sample file servers.txt
:
server1
server2
server3
Working code:
# usage example: ./test.pl Jul 05 2010 <server> <logfile>
# $ARGV[0]=Jul
# $ARGV[1]=05
# $ARGV[2]=2010
# $ARGV[3]=server
# $ARGV[4]=/pathoffile
use strict;
use warnings;
my($mon,$day,$year,$server,$file) = @ARGV;
open(my $fh,"ssh $server cat $file |") or die "can't open log $server:$file: $!\n";
while (my $line = <$fh>) {
if ($line =~ /.* $mon $day \d{2}:\d{2}:\d{2} $year:.*(ERROR:|backup-date=|backup-size=|backup-time=|backup-status)/) {
print $line;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来像是 xargs 的工作。
使用此替身来显示执行运行的命令的
test.pl
This looks like a job for
xargs
.Using this stand-in for your
test.pl
that shows the command executedrunning
produces