Solved

Help with my perl script

Posted on 2009-04-14
21
198 Views
Last Modified: 2012-05-06
hello, I have this script but it fails to run. It was orginaly set to run on a unix box( See below), so I changed the path to my my windows path wihere my input file is located.

When I run it, it fails to look at my directory where the files are located.

My input file is located at c:\named.conf-rich

Can someone fix this so it will run on my windows box.

Thanks

#!/perl
 
unless ($ARGV[0])  {
    print "******************************************************\n";
    print "*                                                    *\n";
    print "*                                                    *\n";
    print "******************************************************\n";
    exit;
}
 
$inputfile=$ARGV[0];
$zonefound = 0;
 
open(INFILE, $inputfile) or die "Unable to find $inputfile\n";
@conf = <INFILE>;
close INFILE;
 
foreach $linein (@conf)  {
    chomp($linein);
    $count++;
 
        if ($linein =~ /^zone\s+\".*in-addr.arpa\"/)  {
            $countZONE++;
            $zonefound = 1;
            ($j1, $j2, $j3, $j4, $j5) = split " ",$linein;
#print "ZONE-START: $linein\n";
        }
        elsif (($zonefound == 1) and ($linein =~ /^\}/))  {
            $zonefound = 0;
#print "  ZONE-END: $linein\n";
        }
        elsif (($zonefound == 1) and ($linein =~ /allow-update/))  {
print "$j2,$linein\n";
        }
        else {
                next;
        };
 
};

Open in new window

0
Comment
Question by:richsark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 7
  • 3
  • +1
21 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 24141885
Do you get an error when you run it?
0
 
LVL 1

Author Comment

by:richsark
ID: 24142031

Hi,
Well, first I am unsure of the $input file in terms of what I need to set. I do know that my input file is located in c:\named.conf\-rich

In that directory I have tons of file called
named.conf.6cmghnsrt01.netdevice.rich.com

What this script will do is look at the contents of what is directed.

So, I want to make sure when I run this script, I need it to look at the directory c:\named.conf\-rich and look at all the files that begin with named.conf and pull out what the script does.

I think the script runs with script name and filename, checkconf.pl

named.conf.6cmghnsrt01.netdevice.rich.com
 
I do not want it to do that. I want it to look in my dir and do it automaticlay.
Thanks
0
 
LVL 84

Expert Comment

by:ozo
ID: 24142113
do you want to read @ARGV=<c:/named.conf/-rich/*>?
how would you have done what you are trying to do in unix?
the code snippet does not seem to match your description.
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 1

Author Comment

by:richsark
ID: 24142221
Hi, yes, My script for unix is what I want to make work for my needs. The script I have is a baseline which I think is close to what I need.
So, yes my input file is c:\named.conf-rich which will contain all the files I need for this script to read.
Thanks Ozo
0
 
LVL 1

Author Comment

by:richsark
ID: 24142766
hi, does that help?
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24143146
The script you posted expects you to give it the name of a file as a command line argument, like so:
    script.pl c:\named.conf-rich\list.txt

The script then reads that file, and counts the number of zones (lines beginning with "zone") in them.  
0
 
LVL 1

Author Comment

by:richsark
ID: 24143175
Hi Adam, I know , I want it to read a specific directory and work from there. That is why I needed this script to be slightly re-worked.
I rather have this script work in a wau where I excute the script, it will look in my directory, in this case c:\named.conf-rich
In there it will find all the files that start with named.conf.xxxxx and spit out what its designed to do from there.
Does that sound do'able
0
 
LVL 84

Expert Comment

by:ozo
ID: 24143225
#open(INFILE, $inputfile) or die "Unable to find $inputfile\n";
#@conf = <INFILE>;
#close INFILE;
 
#foreach $linein (@conf)  {

@ARGV=<$inputfile/named.conf.*>;
while( $linein=<> ){

0
 
LVL 1

Author Comment

by:richsark
ID: 24143265
Hi Ozo, so this will go like this in my scirpt
#!/perl
 
unless ($ARGV[0])  {
    print "******************************************************\n";
    print "*                                                    *\n";
    print "*                                                    *\n";
    print "******************************************************\n";
    exit;
}
 
#open(c:\named.conf-rich, $inputfile) or die "Unable to find $inputfile\n";
#@conf = <INFILE>;
#close INFILE;
 
#foreach $linein (@conf)  {
 
@ARGV=<$c:/named.conf-rich/named.conf.*>;
while( $linein=<> ){ 
foreach $linein (@conf)  {
    chomp($linein);
    $count++;
 
        if ($linein =~ /^zone\s+\".*in-addr.arpa\"/)  {
            $countZONE++;
            $zonefound = 1;
            ($j1, $j2, $j3, $j4, $j5) = split " ",$linein;
#print "ZONE-START: $linein\n";
        }
        elsif (($zonefound == 1) and ($linein =~ /^\}/))  {
            $zonefound = 0;
#print "  ZONE-END: $linein\n";
        }
        elsif (($zonefound == 1) and ($linein =~ /allow-update/))  {
print "$j2,$linein\n";
        }
        else {
                next;
        };
 
};

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 24143304
no,
#foreach $linein (@conf)  {
should be  commented out
and $c would need to be defined.
If you are not reading $c = $ARGV[0];
instead of $inputfile=$ARGV[0];
then you could code the path you want into the <>
0
 
LVL 1

Author Comment

by:richsark
ID: 24143326
Hi Ozo, can you please put the final code below so there is not to much back and forth.
I appericate it Ozo/Adam
Thanks
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 230 total points
ID: 24143373
#!/perl
 
#unless ($ARGV[0])  {
#    print "******************************************************\n";
#    print "*                                                    *\n";
#    print "*                                                    *\n";
#    print "******************************************************\n";
#    exit;
#}
 
#$inputfile=$ARGV[0];
$zonefound = 0;
 
#open(INFILE, $inputfile) or die "Unable to find $inputfile\n";
#@conf = <INFILE>;
#close INFILE;
 
#foreach $linein (@conf)  {

@ARGV=<c:/named.conf-rich/named.conf.*>;  #it looks like you want to ignore $inputfile=$ARGV[0];
 and always look for c:/named.conf-rich/named.conf.*
while( $linein=<> ){

    chomp($linein);
    $count++;
 
        if ($linein =~ /^zone\s+\".*in-addr.arpa\"/)  {
            $countZONE++;
            $zonefound = 1;
            ($j1, $j2, $j3, $j4, $j5) = split " ",$linein;
#print "ZONE-START: $linein\n";
        }
        elsif (($zonefound == 1) and ($linein =~ /^\}/))  {
            $zonefound = 0;
#print "  ZONE-END: $linein\n";
        }
        elsif (($zonefound == 1) and ($linein =~ /allow-update/))  {
print "$j2,$linein\n";
        }
        else {
                next;
        };
 
};
0
 
LVL 1

Author Comment

by:richsark
ID: 24143412
Awsome, so where it says
#@conf = <INFILE>;
I need to put:
@conf = <c:\named.conf-rich>;
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24143436
In the new code, @conf isn't used.  This line:
    @ARGV=<c:/named.conf-rich/named.conf.*>;
is the line that searches for the files named c:\named.conf-rich\named.conf.*.

Then this line:
    while( $linein=<> ){
is what reads from those files, one line at a time.
0
 
LVL 84

Expert Comment

by:ozo
ID: 24143452
No, that's not what I wrote.
But now I'm getting confused
But is c:\named.conf-rich a text file or a directory
0
 
LVL 1

Author Comment

by:richsark
ID: 24143489
ok, so we can assume this puppy will work as below
#!/perl
 
#unless ($ARGV[0])  {
#    print "******************************************************\n";
#    print "*                                                    *\n";
#    print "*                                                    *\n";
#    print "******************************************************\n";
#    exit;
#}
 
#$inputfile=$ARGV[0];
$zonefound = 0;
 
#open(INFILE, $inputfile) or die "Unable to find $inputfile\n";
#@conf = <INFILE>;
#close INFILE;
 
#foreach $linein (@conf)  {
 
@ARGV=<c:/named.conf-rich/named.conf.*>;  #i ignore $inputfile=$ARGV[0];
 and always look for c:/named.conf-rich/named.conf.*
while( $linein=<> ){
 
    chomp($linein);
    $count++;
 
        if ($linein =~ /^zone\s+\".*in-addr.arpa\"/)  {
            $countZONE++;
            $zonefound = 1;
            ($j1, $j2, $j3, $j4, $j5) = split " ",$linein;
#print "ZONE-START: $linein\n";
        }
        elsif (($zonefound == 1) and ($linein =~ /^\}/))  {
            $zonefound = 0;
#print "  ZONE-END: $linein\n";
        }
        elsif (($zonefound == 1) and ($linein =~ /allow-update/))  {
print "$j2,$linein\n";
        }
        else {
                next;
        };
 
};

Open in new window

0
 
LVL 39

Accepted Solution

by:
Adam314 earned 250 total points
ID: 24143538
Cleaning up your code...
#!/perl
 
$zonefound = 0;
@ARGV=<c:/named.conf-rich/named.conf.*>;
 
while( $linein=<> ){
    chomp($linein);
    $count++;
    if ($linein =~ /^zone\s+\".*in-addr.arpa\"/)  {
        $countZONE++;
        $zonefound = 1;
        ($j1, $j2, $j3, $j4, $j5) = split " ",$linein;
    }
    elsif (($zonefound == 1) and ($linein =~ /^\}/))  {
        $zonefound = 0;
    }
    elsif (($zonefound == 1) and ($linein =~ /allow-update/))  {
        print "$j2,$linein\n";
    }
    else {
        next;
    }
}

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 24143617
Ok, will get back when a few
0
 
LVL 84

Expert Comment

by:ozo
ID: 24143635
#If I'm understanding what you are doing, it looks like your entire program may be

@ARGV=<c:/named.conf-rich/named.conf.*>;
(($j2)=/^zone\s+(".*in-addr.arpa")/) .. /^}/ and /allow-update/ and print "$j2,$_"  while <>;

0
 
LVL 5

Assisted Solution

by:vikaskhoria
vikaskhoria earned 20 total points
ID: 24145462
I guess the basic problem you were facing initially is like this:
When you were using arguments for giving the path, then you were entering the path with front slashes '\'
Whereas for perl script you should have done it with '/'

So the argument should have been 'c:/named.conf-rich' instead of 'c:\named.conf-rich'.
Rest of the logic changes for reading all files in the directory is well suggested by experts above.
I just added this, in case you again want to take user input for path, instead of hard coding.
0
 
LVL 1

Author Closing Comment

by:richsark
ID: 31570115
Excellent work !
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
convert Systemjs to Webpack 3 130
Interactive Powershell script to reset password age in AD 4 65
Powershell GUI 11 98
Notepad++ Question 11 28
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question