• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

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

0
Dondadda533
Asked:
Dondadda533
4 Solutions
 
TintinCommented:
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

0
 
NovaDenizenCommented:
Usually you start with the first option and go from there.  The 'shift' shell builtin is very useful for managing command line arguments.  

You should put these commands in a loop.  I'm using them serially just for explanatory purposes.

# get the first argument
echo first is $1
# shift drops the first argument and shifts the rest of the arguments down one.
shift
echo second is $1
shift
echo third is $1
0
 
ahoffmannCommented:
even it is homework, you provided code, so lets improve it:

you need a loop like following:

while [ $# -gt  0 ]; do
  case "$1" in
...
  esac
  shift
done
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Dondadda533Author Commented:
Can someone show me an example of how to use the getopts function
0
 
TintinCommented:
Here's a getopts example:

while getopts Dl:v opt
do
  case $opt in
  D)    DEBUG=TRUE
        set -x
        ;;
  l)    LOGFILE=$OPTARG
        ;;
  v)    VERBOSE=1
        ;;
  \?)   echo "Invalid option: $opt" ; exit 1
        ;;
  esac
done
0
 
manav_mathurCommented:
<quote>
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.
</quote>

For a command line like above, the getopts would be designed as

while getopts ":f:d:p:n:" opt
do
  case $opt in
  f)    LOG_FILE_NAME=$optarg;;
  d)    DATE=$optarg;;
  p)    CURR_PAGE=$optarg;;
  n)   IP_ADDRESS=$optarg;;
  \?)   echo "Invalid option: $opt" ; exit 1 ;;
  esac
done
0
 
TintinCommented:
manav_mathur

s/optarg/OPTARG/

case matters.
0
 
manav_mathurCommented:
Thanx for the correction Tintin.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now