Listing 2 The CommandLength.pl perl script
#!/usr/bin/perl -w
# $Id$
#
# Please note that this is alpha code
# Provided without any guarantees
#
# Programmer: Mihalis Tsoukalos
# Date: Monday 21 March 2006
#
# Command line arguments
# program_name.pl <directory with history files>
use strict;
my $directory = "";
my $filename = "";
# The variables for storing total number of commands
# by command length.
my $CAT1 = 0;
my $CAT2 = 0;
my $CAT3 = 0;
my $CAT4 = 0;
my $CAT5 = 0;
die <<Thanatos unless @ARGV;
usage:
$0 directory
Thanatos
if ( @ARGV != 1 )
{
die <<Thanatos
usage info:
Please use exactly 1 argument!
Thanatos
}
# Get the directory name
($directory) = @ARGV;
print "The following text files found in directoy: $directory\n";
opendir(BIN, $directory)
|| die "Error opening directory $directory: $!\n";
while (defined ($filename = readdir BIN) )
{
# The following command does not process . and ..
next if( $filename =~ /^\.\.?$/ );
print $filename."\n";
process_file($directory."/".$filename);
}
print "Category 1: $CAT1\n";
print "Category 2: $CAT2\n";
print "Category 3: $CAT3\n";
print "Category 4: $CAT4\n";
print "Category 5: $CAT5\n";
exit 0;
sub process_file
{
my $file = shift;
my $line = "";
open (HISTORYFILE, "< $file")
|| die "Cannot open $file: $!\n";
while ( defined($line = <HISTORYFILE>) )
{
chomp $line;
next if ( ! defined($line) );
check_category($line);
}
}
sub check_category
{
my $command = shift;
chomp $command;
my $length = length($command);
if ( $length <= 2 )
{
$CAT1 ++;
}
elsif ( $length <= 5 )
{
$CAT2 ++;
}
elsif ( $length <= 10 )
{
$CAT3 ++;
}
elsif ( $length <= 15 )
{
$CAT4 ++;
}
else
{
$CAT5 ++;
}
}
|