开发者

How to pass argument to ARGV from a text file in Perl

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;
    }
}


This looks like a job for xargs.

Using this stand-in for your test.pl that shows the command executed

#! /usr/bin/perl

use warnings;
use strict;

$" = "][";
print "[@ARGV]\n";

running

$ xargs -I{} ./test.pl Jul 05 2010 {} logfile <servers.txt

produces

[Jul][05][2010][server1][logfile]
[Jul][05][2010][server2][logfile]
[Jul][05][2010][server3][logfile]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜