How would one go about optimizing this current .sh program so it works at a more minimal time. Such as is there a better way to count what I need than what I have done or better way to match patterns in the file? Thanks,
#declare variables to be used.
help=-1
count=0
JanCount=0
FebCount=0
MarCount=0
AprCount=0
MayCount=0
JunCount=0
JulCount=0
AugCount=0
SepCount=0
OctCount=0
NovCount=0
DecCount=0
# process all arguments (loop while $1 is present)
while [ -n "$1" ] ; do
case $1 in
-h*|-H*) echo "Enter in a Year or a Month and Year to count the number of
accesses made during that period." ; help=0 ;
shift ;;
[12][0-9][0-9][0-9]) the_year=$1 ;
shift ;;
Jan*|jan*|JAN*) the_month="Jan" ; shift ;;
Feb*|feb*|FEB*) the_month="Feb" ; shift ;;
Mar*|mar*|MAR*) the_month="Mar" ; shift ;;
Apr*|apr*|APR*) the_month="Apr" ; shift ;;
May*|may*|MAY*) the_month="May" ; shift ;;
Jun*|jun*|JUN*) the_month="Jun" ; shift ;;
Jul*|jul*|JUL*) the_month="Jul" ; shift ;;
Aug*|aug*|AUG*) the_month="Aug" ; shift ;;
Sep*|sep*|SEP*) the_month="Sep" ; shift ;;
Oct*|oct*|OCT*) the_month="Oct" ; shift ;;
Nov*|nov*|NOV*) the_month="Nov" ; shift ;;
Dec*|dec*|DEC*) the_month="Dec" ; shift ;;
*) echo "? unrecognised input: $1" ;
shift ;;
esac
done
# read the file to count accesses made
oldIFS="$IFS"
while IFS="/:" read day month year rest
do
MONTH=$month
YEAR=$year
if [ "$the_year" == "$year" ] && [ "$the_month" == "$month" ]
then count=`expr $count + 1`
elif [ "$the_year" == "$year" ] && [ "$the_month" == "" ]
then if [ "$month" = "Jan" ]
then JanCount=`expr $JanCount + 1`
elif [ "$month" = "Feb" ]
then FebCount=`expr $FebCount + 1`
elif [ "$month" = "Mar" ]
then MarCount=`expr $MarCount + 1`
elif [ "$month" = "Apr" ]
then AprCount=`expr $AprCount + 1`
elif [ "$month" = "May" ]
then MayCount=`expr $MayCount + 1`
elif [ "$month" = "Jun" ]
then JunCount=`expr $JunCount + 1`
elif [ "$month" = "Jul" ]
then JulCount=`expr $JulCount + 1`
elif [ "$month" = "Aug" ]
then AugCount=`expr $AugCount + 1`
elif [ "$month" = "Sep" ]
then SepCount=`expr $SepCount + 1`
elif [ "$month" = "Oct" ]
then OctCount=`expr $OctCount + 1`
elif [ "$month" = "Nov" ]
then NovCount=`expr $NovCount + 1`
elif [ "$month" = "Dec" ]
then DecCount=`expr $DecCount + 1`
fi
fi
done < my_sample_log2.txt
IFS=$oldIFS
# print out the accesses
if [ "$help" != 0 ]
then if [ "$count" != "0" ]
then echo "Report:" $count "accesses in $the_month $the_year."
else
echo "Report for the year $the_year:"
echo "Jan:" $JanCount "accesses."
echo "Feb:" $FebCount "accesses."
echo "Mar:" $MarCount "accesses."
echo "Apr:" $AprCount "accesses."
echo "May:" $MayCount "accesses."
echo "Jun:" $JunCount "accesses."
echo "Jul:" $JulCount "accesses."
echo "Aug:" $AugCount "accesses."
echo "Sep:" $SepCount "accesses."
echo "Oct:" $OctCount "accesses."
echo "Nov:" $NovCount "accesses."
echo "Dec:" $DecCount "accesses."
fi
fi