Hello everyone. I had used Perl a little bit long time ago and now I'm trying to write a little program, but I have got stuck. I would like to check bunch of kickstart files for a '--hostname=' pattern. If it matches I want to get the collumn right after it (actual hostname). Else it should take the name of the file (stripped of '.ks' suffix) and save all of them to the array. Commented lines should not be taken. This is how I have done it in bash + awk
#!/bin/env bash
declare -a hostlist=$(grep -P '(?<!#)--hostname' *.ks | awk -F'=' '{pr
+int $2}')
declare -a not_predefined_hosts=$(grep -L "\-\-hostname" *.ks | cut -d
+\. -f1 )
declare -a commented=$(grep -l ".*#.*\-\-hostname" *.ks | cut -d\. -f1
+ )
for host in "${not_predefined_hosts[@]}"
do
hostlist+="$host"
done
for host in "${commented[@]}"
do
hostlist+="$host"
done
This works, but when I want to do it with perl, I get multiple issues.
#!/usr/bin/env perl
use strict;
use warnings;
use File::Basename;
my $lab_root = dirname $0;
opendir( DH, $lab_root) or die "Cannot open $lab_root: $!\n";
my @kickstarts = grep ( /\.ks$/, readdir(DH));
my @bsname ;
my $hostname;
for my $kickstart (@kickstarts) {
my $name = (split /\./)[0], $kickstart;
open my $fh, $kickstart or die "Cannot open $kickstart: $!";
while (<$fh>) {
chomp;
my @fields = split /=/;
if ( $fields[0] eq '--hostname') {
$hostname = $fields[1];
print "$hostname\n";
push @bsname , $hostname;
}else {
$hostname = $name;
print "$hostname\n";
push @bsname , $hostname;
}
close $fh;
}
}
First, I get the warnings
Provisioner]$ ./installer.pl
Useless use of private variable in void context at ./installer.pl line
+ 13.
Use of uninitialized value $_ in split at ./installer.pl line 13.
Use of uninitialized value $hostname in concatenation (.) or string at
+ ./installer.pl line 24, <$fh> line 1.
readline() on closed filehandle $fh at ./installer.pl line 27.
Use of uninitialized value $_ in split at ./installer.pl line 13.
master.myhost.com
readline() on closed filehandle $fh at ./installer.pl line 27.
Use of uninitialized value $_ in split at ./installer.pl line 13.
Use of uninitialized value $hostname in concatenation (.) or string at
+ ./installer.pl line 24, <$fh> line 1.
readline() on closed filehandle $fh at ./installer.pl line 27.
Use of uninitialized value $_ in split at ./installer.pl line 13.
Use of uninitialized value $fields[0] in string eq at ./installer.pl l
+ine 18, <$fh> line 1.
Use of uninitialized value $hostname in concatenation (.) or string at
+ ./installer.pl line 24, <$fh> line 1.
readline() on closed filehandle $fh at ./installer.pl line 27.
I don't understand why I'm getting "Use of uninitialized value" warnings, when I initialize all variables with "my" beforehand. Also, why does the filehandle "$fh" close before the close statement? And what the "Useless use of private variable in void context" mean? All examples I was able to google on it were very different from my code and didn't help me to understand that.
Second, when the code runs,( with added 'print @bsname;' ) it matches only once (but strangely prints twice). There are multiple kickstart files with "--hostname" directive in it, but it ignores the rest. Also the "else" part doesn't work (none of the file names are matched).
[################### Provisioner]$ ./installer.pl
master.myhost.com
master.myhost.com[################### Provisioner]$
Can somebody please point out mistake in my code? I'm sure it will be something trivial, but I can't wrap my head around it. Thanks!