The code you posted doesn't compile. Please post a compiling code snippet and also a sample of what you have in /etc/services. (See Write-up formatting tips.)
Here's a rewrite of your code with several comments so you can see what's different:
( Edit: as hippo points out below, it's not likely that your /etc/services file actually contains lines containing only port numbers. This example code does not address that issue, nor try to use regular expression matching; it's just an example of how to work through your two data sets. )
#!usr/bin/perl use strict; # don't leave home without it use warnings; # same here! use 5.10.0; # so you can use say() my @ports = qw( 21 22 23 ); # make a hash from the array for easier lookup my %ports = map { $_ => 1 } @ports; # use a lexical variable for your filehandle open my $SERVICES_FILE, '<', '/etc/services' or die $!; # read one line at a time with while() while ( my $line = <$SERVICES_FILE> ) { # deal with new-line character at end of line chomp( $line ); # 'postfix if' simplifies syntax say "Match Found: $line" if $ports{ $line }; } # good practice to close filehandle close $SERVICES_FILE or die $!; # good practice to tell Perl when the program's done __END__
In reply to Re: Port check - Services File
by 1nickt
in thread Port check - Services File
by deelinux
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |