blackice69 has asked for the wisdom of the Perl Monks concerning the following question:
Hello.. I asked a question here earlier about making a logfile and reportfile command line argument type script. Here is what I have.
#!C:\Perl\Bin\Perl.exe # Enable Pragmatic Modules use warnings ; use strict; # Define Variables and Hashes my $numelements ; my $done = 3 ; my $count = 0 ; my $username = "" ; my %userhash ; my $inputfile = "" ; my $outputfile = "" ; my $userlist = "" ; my $record = "" ; my $bigcounter = 0 ; # Argument testing $numelements = @ARGV ; print "\n$numelements\n" ; # Check for 0 arguments and switches, apply error message if true if ( $numelements == 0 ) { print "\n *** Error : Command-line switches and arguments must be p +rovided. ***\n\n" ; print " $0 -l <log_file> -u <userlist_file> -r <report_file>\n\n" ; print " -l <log_file> The relative or absolute path to +the webserver logfile.\n" ; print " -u <userlist_file> The relative or absolute path to +the user list file.\n" ; print " -r <report_file> The relative or absolute path to +the generated report file.\n\n" ; exit 200 ; } # Check to osee that they gave more or less than 6 arguments, appply a +pproproiate error message. if (( $numelements > 0 ) and ( $numelements !~ 6 )) { print "\n *** Error : Too many (or too few) command-line switches a +nd arguments were provided. ***\n\n" ; print " $0 -l <log_file> -u <userlist_file> -r <report_file>\n\n" ; print " -l <log_file> The relative or absolute path to +the webserver logfile.\n" ; print " -u <userlist_file> The relative or absolute path to +the user list file.\n" ; print " -r <report_file> The relative or absolute path to +the generated report file.\n\n" ; exit 201 ; } # Check for double switches if (( $ARGV[0] =~ $ARGV[2] ) or ( $ARGV[2] =~ $ARGV[4] ) or ( $ARGV[0] =~ $ARGV[4] )) { print "\n *** Error : Incomplete (or incorrect) command-line sw +itches and arguments were provided. ***\n\n" ; print " $0 -l <log_file> -u <userlist_file> -r <report_file>\n\ +n" ; print " -l <log_file> The relative or absolute path + to the webserver logfile.\n" ; print " -u <userlist_file> The relative or absolute path + to the user list file.\n" ; print " -r <report_file> The relative or absolute path + to the generated report file.\n\n" ; exit 202 ; } # Start a loop to check for all correct switches, and apply input file +names to variables while ( $done != 0 ) { if ( $count == 6 ) { print "\n *** Error : Incomplete (or incorrect) command-line swit +ches and arguments were provided. ***\n\n" ; print " $0 -l <log_file> -u <userlist_file> -r <report_file>\n\n" + ; print " -l <log_file> The relative or absolute path t +o the webserver logfile.\n" ; print " -u <userlist_file> The relative or absolute path t +o the user list file.\n" ; print " -r <report_file> The relative or absolute path t +o the generated report file.\n\n" ; exit 202 ; } if ( $ARGV[$count] =~ /^-l$/i ) { $inputfile = $ARGV[$count + 1] ; $done-- ; } if ( $ARGV[$count] =~ /^-u$/i ) { $userlist = $ARGV[$count + 1] ; $done-- ; } if ( $ARGV[$count] =~ /^-r$/i ) { $outputfile = $ARGV[$count + 1] ; $done-- ; } $count = $count + 2 ; } # Check for input file existance and begin to parse file, otherwise di +splay appropriate error message if ( -e $inputfile ) { open( INPUTDATAFILE, "< $inputfile" ) or die " \n\nError: $!\n\n" ; while ( defined( $record = <INPUTDATAFILE> ) ) { if ( $bigcounter == 100000 ) { print "." ; $bigcounter = 0 ; } chomp( $record ) ; if($record =~ /\/\~(c(s|e)[0-9][0-9][0-9][0-9])/) { $username = $1 ; if(exists $userhash{$username}) { $userhash{$username}++; } else{ $userhash{$username}=1 ; } } $bigcounter++ ; } $numelements = %userhash ; print "$numelements\n" ; }else { print "\n *** Error : Webserver logfile '$inputfile' : NOT FOUND. * +**\n\n" ; exit 100 ; } # Check for userlist file, or display apropriate error message if ( -e $userlist ) { }else { print "\n *** User list file '$userlist' : NOT FOUND. ***\n\n" ; exit 101 ; } system "( Pause ) " ;
Now.. My problem is.. I want to find a different way to do the following:
How Would I accomplish this? I am being told this is an odd way to do this.# Start a loop to check for all correct switches, and apply input fil +enames to variables while ( $done != 0 ) { if ( $count == 6 ) { print "\n *** Error : Incomplete (or incorrect) command-line swit +ches and arguments were provided. ***\n\n" ; print " $0 -l <log_file> -u <userlist_file> -r <report_file>\n\n" + ; print " -l <log_file> The relative or absolute path t +o the webserver logfile.\n" ; print " -u <userlist_file> The relative or absolute path t +o the user list file.\n" ; print " -r <report_file> The relative or absolute path t +o the generated report file.\n\n" ; exit 202 ; } if ( $ARGV[$count] =~ /^-l$/i ) { $inputfile = $ARGV[$count + 1] ; $done-- ; } if ( $ARGV[$count] =~ /^-u$/i ) { $userlist = $ARGV[$count + 1] ; $done-- ; } if ( $ARGV[$count] =~ /^-r$/i ) { $outputfile = $ARGV[$count + 1] ; $done-- ; } $count = $count + 2 ; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Script Help!
by kcott (Archbishop) on Nov 02, 2010 at 17:33 UTC | |
|
Re: Script Help!
by Utilitarian (Vicar) on Nov 02, 2010 at 17:18 UTC | |
|
Re: Script Help!
by kcott (Archbishop) on Nov 03, 2010 at 06:59 UTC | |
by raybies (Chaplain) on Nov 03, 2010 at 12:23 UTC | |
|
Re: Script Help!
by eff_i_g (Curate) on Nov 02, 2010 at 19:19 UTC |