Alright, so the question heading might be a bit confusing. I have replicated my actual problem in below code:
I am creating files with two different IP addresses as a prefix and then I want to list the files only with a particular ip address prefix:
I am creating files with "10.10.10.10" and "11.11.11.11" as prefix and then I want to list files only with the "10.10.10.10" prefix.
Here is my code:
#!/usr/bin/perl -w
use strict;
my $ipaddr1 = '10.10.10.10';
for ( my $counter = 1; $counter < 10; $counter++)
{
my $filename = $ipaddr1."_".$counter.".txt";
open (my $fh, ">", $filename) or die "$! \n";
}
my $ipaddr2 = '11.11.11.11';
for ( my $counter = 1; $counter < 10; $counter++)
{
my $filename = $ipaddr2."_".$counter.".txt";
open (my $fh, ">", $filename) or die "$! \n";
}
print "Done with creating hte files. \n";
print "Lets print the files with only 10.10.10.10 prefix \n";
for my $var ( `ls -1 $ipaddr1_*` ) ### <<< this is the probelm
{
print $var;
}
Output:
Global symbol "$ipaddr1_" requires explicit package name at test.pl line 21.
Execution of test.pl aborted due to compilation errors.
So, I fully understand the error that it is not able to interpret the perl variable while executing a bash command.
Question:
how to achieve this? I do not want to pass explicit IP address to the ls -1 command but instead want to pass the variable which holds the respective IP address.
Thanks.
<>