[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need Help with my Shell script Assignment

Posted on 2005-04-10
11
Medium Priority
?
308 Views
Last Modified: 2013-12-26
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
Comment
Question by:Dondadda533
8 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 13749972
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
 
LVL 22

Accepted Solution

by:
NovaDenizen earned 500 total points
ID: 13753390
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
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 500 total points
ID: 13754421
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Dondadda533
ID: 13756302
Can someone show me an example of how to use the getopts function
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 500 total points
ID: 13758307
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
 
LVL 16

Assisted Solution

by:manav_mathur
manav_mathur earned 500 total points
ID: 13763848
<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
 
LVL 48

Expert Comment

by:Tintin
ID: 13767776
manav_mathur

s/optarg/OPTARG/

case matters.
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 13769853
Thanx for the correction Tintin.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question