1753524 Members
4968 Online
108795 Solutions
New Discussion юеВ

perl script help

 
SOLVED
Go to solution
rhansen
Frequent Advisor

perl script help

Hello,

Attached below is my code. I would like to add an option so that I can get the results for all the 5 regions at once instead of running the script 5 times. An option like run it for all the regions.

Thanks in advance.

#!/usr/bin/perl

use strict;
use warnings;

## Machine Number hashes

my $regionMachine = {'01' =>['pl010200','pl010300','pl010400','pl010500'],
'02' => ['pl020200','pl020300','pl020400','pl020500'],
'03' => ['pl030200','pl030300','pl030400','pl030500','pl030600','pl030700','pl030800'],
'04' => ['pl040200', 'pl040300', 'pl040400'],
'05' => ['pl050200', 'pl050300', 'pl050400', 'pl050500', 'pl050600', 'pl050700', 'pl050800'],

};

## Set the data file on remote machine
my $auditFilePath = "/usr/local/bin/mpi.lis";


## Get the home dir for user
my $home = ( $ENV{HOME} );
chomp ($home);

## Date in MMDDYYYY
my $dateFormat = `date +%m%d%Y`;
chomp($dateFormat);

my @regions = get_user_input();

foreach my $region(@regions){

## Get servers based on region chosen
my @servers=();
push(@servers, @{$regionMachine->{$region}});

## File creation in user's home directory
my $filename = $home."/region".$region;

foreach my $server(@servers){

my $HANDLE;
open(HANDLE,">>", $filename) or die "Could not open $filename";

## Get the file contents from remote machine
my $str=`ssh -l user $server cat $auditFilePath`;
my @records = split("\n",$str);

foreach my $record(@records){
my @fields = split(":",$record);
if (@fields < 6) {
warn "Warning: '$record' has fewer than 6 fields\n";
next;
}

## Store the required data in file
print HANDLE $fields[0].":".$fields[1].":".$fields[2].":".$fields[3].":".$fields[5].":".$server." \n";

}
close HANDLE;
## Changing file permissions to 0755
chmod(0755, "$filename");
}
}

print "\nYour request has been completed\n";

sub get_user_input
{

my $date = `date`;
chomp($date);

print "\033[2J";

print "#########################################################\n";
print "# #\n";
print "# WELCOME #\n";
print "# $date #\n";
print "# #\n";
print "# #\n";
print "#########################################################\n";


print "\nChoose from the option given below: \n\n";
print "1. Select this option to continue \n\n";
print "2. exit \n\n";
print "Please make your selection:";


my $response = <>;
chomp ($response);
if ($response eq '2'){
print "Bye \n";
exit;
}elsif ($response eq '1'){
return main_input();
}
if ( ($response ne 1) || ($response ne 2) ){
return get_user_input();
}

}



sub main_input{

my $continue = "yes";
my @regions=();

while ($continue eq 'yes'){
print "\nPlease enter the region number.";
my $region = <>;
chomp ($region);

if ( ($region < 1) || ($region > 05) || (length($region) > 2) {
print "Invalid entry. Program will now exit\n";
exit;
}

if(length($region) ==1){
$region = "0".$region;
}

push(@regions, $region);


$continue="no";
}

print "\nProcessing your request\n";
return @regions;

}
2 REPLIES 2
James R. Ferguson
Acclaimed Contributor
Solution

Re: perl script help

Hi:

For openers, you script has a syntax error. Line-116 reads:

if ( ($region < 1) || ($region > 05) || (length($region) > 2) {

...it needs a closing parentheses and can be cleaned up to :

if ( $region < 1 || $region > 5 || length($region) > 2) ) {

As for your question, you should be able to make a subroutine to encapsulate the region of code from:

## Set the data file on remote machine
...
print "\nYour request has been completed\n";

Then, simply add a option to trigger that subroutine. For example, call the subroutine 'dowork' and pass to it the value for which it needs to do work:

...
sub dowork {
my ($value) =@_;
...

Call it like:

dowork($_) for (1..5);

...when you want to do "ALL" regions.

Regards!

...JRF...


rhansen
Frequent Advisor

Re: perl script help

Thanks.