Link to home
Start Free TrialLog in
Avatar of Dondadda533
Dondadda533

asked on

Need Help with my Shell script Assignment

here is my assignment, I am stuck on how to detect which options were chosen ...
This is what I have so far:

#!/bin/bash

if [ $# -eq 2 ]
then
      if [ $1 = "-d" ]
      then
            datchars=$(echo $2 | wc -c)
            datchars=`expr $datchars -1`
            if [ $datchars = 8 ]
            then
                  day=$(echo $2 | cut -c,0-2)
                  month=$(echo $2 | cut -c,3-4)
                  year=$(echo $2 | cut -c,5-8)
                  mm=1
            elif [ $datchars = 6 ]
            then
                  month=$(echo $2 | cut -c,0-2)
                  year=$(echo $2 | cut -c,3-6)
                  mm=1
            elif [ $datchars = 4 ]
                  year=$(echo $2 | cut -c,0-4)
            else
                  echo INVALID TIME PERIOD!
            fi
            if [ $mm = 1 ]
                  then
                        case $month in
                        1)
                              damonth="Jan"
                              ;;
                        2)
                              damonth="Feb"
                              ;;
                        3)
                              damonth="Mar"
                              ;;
                        4)
                              damonth="Apr"
                              ;;
                        5)
                              damonth="May"
                              ;;
                        6)
                              damonth="Jun"
                              ;;
                        7)
                              damonth="Jul"
                              ;;
                        8)
                              damonth="Aug"
                              ;;
                        9)
                              damonth="Sep"
                              ;;
                        10)
                              damonth="Oct"
                              ;;
                        11)
                              damonth="Nov"
                              ;;
                        12)
                              damonth="Dec"
                              ;;
                        *)
                              echo INVALID MONTH
                              ;;
                        esac
            fi
      
            grep May access_log | wc -l
      fi
fi

--------------------------------------------------------------------------------------------------------------------------------------------

ASSIGNMENT OUTLINE


W3C httpd can log all the incoming requests to an access log file. All log files are generated using the common log file format that several WWW servers use. This provides the possibility of using some tool to generate statistics and to analyze the log file contents.

The access log file contains a log of all the requests made to the server. The name of the log file is usually something like "access_log". Using the Apache server on Linux, the log file is kept (depending on the configuration) in /usr/local/apache/logs/access_log. On Hal, using IBM's www server, the log files are broken into days and stored with names like httpd-log.Jan251998 in /usr/lpp/internet/server_root/logs.

Every time a browser hits your site it leaves a trail in your access log. This file is enough to tell you how many hits you received and gives you some basic information about the browser, such as their hostname.

The traditional format for web log files looks like this:

jupiter.ukweb.com - - [03/Feb/1997:00:06:59 +0000] "GET / HTTP/1.0" 200 4571
jupiter.ukweb.com - - [03/Feb/1997:00:07:00 +0000] "GET /img/awlogo.gif HTTP/1.0" 200 12706

(There are two lines above, both starting with "jupiter.ukweb.com". If you see more than two, the lines have been wrapped on the screen).
This format is called the common log format and is standard across most web servers (although it is not very well documented). There are various tools to analyse data in this format, and it is not too difficult to write custom tools (in, say, perl) to extract the data. But the lack of a common field delimiter makes such tools more complex than necessary and prevents the use of simple Unix programs such as cut.

The common log format is defined like this:

  %h %l %u %t %r %s %b

Where %h is the host address of the user making the request. Additional sequences here are %l (the remote username, if using identd), %u (the HTTP authenticated username, if any), %t (the time in common-log format), %r (the request), %s (the returned status) and %b (the number of bytes in the document served).
For this assignment you are to write a shell script, called wa (web analysis) that may be run from the command-line to perform simple analysis of a www log file. The script should output a number which represents the hit count on a particular web page or from a particular IP address for a given period of time.

The syntax for the command should be as follows:

wa [-f logfile] [-d date] [-p page | -n address]

where

    * [-f logfile]
            optional log file name. The default should be set to something useful.
    * [-d date]
            optional date selection. The default is the current day, month, year, but the user should be able to specify only a month or only a year to get monthly or yearly statistics.
    * [-p page]
            optionally specify statistics for a particular web page.
    * [-n address]
            optionally specify statistics for a particular IP address. Note that -p and -n are mutually exclusive. If neither is specified "all" requests are counted.

For example:
    * wa
            by itself will tell the user the number of hits the default web site got so far on the current day.
    * wa -p selmys/index.html
            Will show how many hits there were on page selmys/index.html on the current day.
    * wa -n 142.204.76.16
            Will show how many hits were requested from 142.204.76.16 so far on the current day.
    * wa -d 10011999
            Will show hits for January 10, 1999
    * wa -d 011999
            Will show hits for January 1999
    * wa -d 1999
            Will show hits for all of 1999

Avatar of Tintin
Tintin

As this is a homework assignment, we aren't allowed to give you a solution, however, we can point you in the right direction.

If you are parsing options, then using getopts is commonly used.

man getopts

ASKER CERTIFIED SOLUTION
Avatar of NovaDenizen
NovaDenizen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dondadda533

ASKER

Can someone show me an example of how to use the getopts function
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
manav_mathur

s/optarg/OPTARG/

case matters.
Thanx for the correction Tintin.