tags:

views:

81

answers:

1

I have a list of arguments to pass into a perl script, using ARGV. The 4th argument, is a servername, 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;
   }

}

+2  A: 

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]
Greg Bacon
@gbacon - This is cool stuff! Thanks for the tip! It works great.
jda6one9
@jaedre619 You're welcome. I'm glad it helped!
Greg Bacon