开发者

Need help with a perl program

Ok, so I am trying take a hash and if any string in an array contains the key(not value actual key name) in the hash discard it. Else print out the string. This issue is with a portion of the findHidden sub routine. I have tried a lot of different things, I will comment below where I have issues. I'm sure someone has an answer, always get one on stack overflow :)

#!/usr/bin/perl
# Configure
use strict;
use warnings;
use Data::Dumper;
#
sub findHidden;
sub GetInfo; 
sub defineHash;
##############

$passwd = '/etc/passwd';
%info = ();

sub GetInfo {
        die "Cannot open: $passwd"
        unless (open(PW,$passwd));
        while(<PW>) {
                chomp;
                my ($uname,$junk1,$junk2,$junk3,$domain,$home) = split(':', $_);
                next unless ($home =~ /vs/);
                %info = (
                        domain  => $domain,
                        home    => "$home/",
                        tmp     => "$home/tmp",
                        htdocs  => "$home/www/htdocs",
                        cgibin  => "$home/www/cgi\-bin",
                );
                print "\n" . $info{domain} . "\n";
                print "+"x40,"\n\n";
                findHidden($info{tmp});
        }
}
sub findHidden {
        defineHash;
        print "Searching " . $_[0] . "\n";
        print "-"x30,"\n\n";
        @hidden = `find $_[0] -iname ".*"`;
        for(@hidden) {
                foreach $key (keys % hExcludes) {
                        if ($_ =~ /$key/){    #
                                last;         # This portion is 
                        }else{                # Only an issue when using more
                                print "$_";   # than 2 keys in my hash.
                                last;
                        }
                }
        }
}
sub defineHash {
        %hExcludes = ();
        %hExcludes = map { $_, 1 } (
                'spamd','.nfs'     # If I add another key here, it breaks.
        );

        %knownExploits = 
                ( );
        print Dumper \%hExcludes;
}
GetInfo;

This Works, and prints out something like this:

/somedir/tmp/.testthis

/somedir/tmp/.sdkfbsdif

/somedir/tmp/.asdasdasd

I understand why It is not working, because it is looping through the keys where some are false and some are positive, I just cannot think of how to make it do what I want, p开发者_开发知识库lease assume I might want to you 10 keys. I know there are ways to do it without using hash key values for my excludes but it is what I want to accomplish.

I have also tried shift @hidden as below to no avail.

                foreach $key (keys % hExcludes) {
                        if ($_ =~ /$key/){    #
                                last;         #
                                shift @hidden;# This portion is 
                        }else{                # Only an issue when using more
                                print "$_";   # than 2 keys in my hash.
                                last;
                        }

Also, keep in mind that things only stop working when I add the third...or more keys.

        %hExcludes = map { $_, 1 } (
                'spamd','.nfs','key3'     # If I add another key here, it breaks
        );


What you need is this:

    @hidden = `find $_[0] -iname ".*"`;
    for(@hidden) {
        undef $isExcluded;
        foreach $key (keys % hExcludes) {
            if ($_ =~ /$key/){
                $isExcluded=1;
                last;
            }
        }
        if( ! $isExcluded ) {
            print "$_";
        }
    }

Whatever happened in your scan through the keys of hExcludes, the code encountered a last on the first key and did not process any more. You need to set a flag and continue iterating until either there are no more keys to set, or a match is found. Then you can print out the values that were not matched.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜