LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 623|回复: 1

大家给个提示

[复制链接]
发表于 2007-4-26 14:42:32 | 显示全部楼层 |阅读模式
习作,程序目的是访问 服务器的logfile,统计出访问人数。然后输出到html文件中。
感觉不太友好,还有对其他系统不太了解,所以如何能更通用,即可移植性更强呢?
提提意见了,谢谢。

#process all arguments (i.e. loop while $1 is present)

usage_help()  #function
{
   echo "usage: Myprogram1 [Log File] [Option]"
   echo
   echo "Options:"
   echo "[year] visit year, e.g. 2007"
   echo "[month] visit month, e.g. Apr"
   echo "Example: Myprogram1 log.dat mar 2007"
   echo "         Myprogram1 log.dat 2007"
}

help_menu() #function
{
   clear
   echo "Description"
   echo "Example: "
}

logfilename=""
the_year=""
the_month=""
totalvisitors=0


if [ $# -lt 1 ]  # -lt: less than [$# -eq 0]
then
   usage_help   # function
   exit 1
fi

if [ "$1" = "-h" ] || [ "$#" = "--h" ] || [ "$#" = "--help" ]
then
   help_menu
   exit 1
fi

#log file exist?
logfilename=$1
if ! [ -a "$logfilename" ]
then
   echo log file "$logfilename not found"
   exit 1
fi
shift

#command arguments
#we have done this in lab 4

#function count visitor
#countVisitors
function countVisitors
{
vline="empty"
totalvisitors=0
Janvisitors=0
Febvisitors=0
Marvisitors=0
Aprvisitors=0
Mayvisitors=0
Junvisitors=0
Julvisitors=0
Augvisitors=0
Sepvisitors=0
Octvisitors=0
Novvisitors=0
Decvisitors=0
{ # data go in this block
until [ -z "$vline" ] #exit if vline
do
   if [ -z "$the_month" ]
      then
         read vline
            case $vline in
               *?Jan/"$the_year"*) let Janvisitors=$Janvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Feb/"$the_year"*) let Febvisitors=$Febvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Mar/"$the_year"*) let Marvisitors=$Marvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Apr/"$the_year"*) let Aprvisitors=$Aprvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?May/"$the_year"*) let Mayvisitors=$Mayvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Jun/"$the_year"*) let Junvisitors=$Junvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Jul/"$the_year"*) let Julvisitors=$Julvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Aug/"$the_year"*) let Augvisitors=$Augvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Sep/"$the_year"*) let Sepvisitors=$Sepvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Oct/"$the_year"*) let Octvisitors=$Octvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Nov/"$the_year"*) let Novvisitors=$Novvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Dec/"$the_year"*) let Decvisitors=$Decvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
            esac
    else
       read vline
          case $vline in
              *?"$the_month"?"$the_year"*) let totalvisitors=$totalvisitors+1 ;;
          esac
   fi
done
} < $logfilename
#echo ${Janvisitors}

#output total visitors
echo "There are $totalvisitors visitors in $the_month $the_year"
if [ -z "$the_month" ]
then
   echo "Total visitor in Jan = $Janvisitors visitors(s)"
   echo "Total visitor in Feb = $Febvisitors visitors(s)"
   echo "Total visitor in Mar = $Marvisitors visitors(s)"
   echo "Total visitor in Apr = $Aprvisitors visitors(s)"
   echo "Total visitor in May = $Mayvisitors visitors(s)"
   echo "Total visitor in Jun = $Junvisitors visitors(s)"
   echo "Total visitor in Jul = $Julvisitors visitors(s)"
   echo "Total visitor in Aug = $Augvisitors visitors(s)"
   echo "Total visitor in Sep = $Sepvisitors visitors(s)"
   echo "Total visitor in Oct = $Octvisitors visitors(s)"
   echo "Total visitor in Nov = $Novvisitors visitors(s)"
   echo "Total visitor in Dec = $Decvisitors visitors(s)"
fi
}

#create webpage
function genWebpage
{

vline="empty"
totalvisitors=0
Janvisitors=0
Febvisitors=0
Marvisitors=0
Aprvisitors=0
Mayvisitors=0
Junvisitors=0
Julvisitors=0
Augvisitors=0
Sepvisitors=0
Octvisitors=0
Novvisitors=0
Decvisitors=0
{ # data go in this block
until [ -z "$vline" ] #exit if vline
do
   if [ -z "$the_month" ]
      then
         read vline
            case $vline in
               *?Jan/"$the_year"*) let Janvisitors=$Janvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Feb/"$the_year"*) let Febvisitors=$Febvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Mar/"$the_year"*) let Marvisitors=$Marvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Apr/"$the_year"*) let Aprvisitors=$Aprvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?May/"$the_year"*) let Mayvisitors=$Mayvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Jun/"$the_year"*) let Junvisitors=$Junvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Jul/"$the_year"*) let Julvisitors=$Julvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Aug/"$the_year"*) let Augvisitors=$Augvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Sep/"$the_year"*) let Sepvisitors=$Sepvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Oct/"$the_year"*) let Octvisitors=$Octvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Nov/"$the_year"*) let Novvisitors=$Novvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
               *?Dec/"$the_year"*) let Decvisitors=$Decvisitors+1 ; let totalvisitors=$totalvisitors+1 ;;
            esac
    else
       read vline
          case $vline in
              *?"$the_month"?"$the_year"*) let totalvisitors=$totalvisitors+1 ;;
          esac
   fi
done
} < $logfilename



#gen a web file
{
        echo "<HTML>"
        echo "<title>Web Statistics</title>"
        echo "<body>"
        echo "<table border=2>"
        echo "<tr>"
        echo "<td>January</td>"
        echo "<td>${Janvisitors}</td>"
        echo "</tr>"
        echo "<td>February</td>"
        echo "<td>${Febvisitors}</td>"
        echo "</tr>"
        echo "<td>March</td>"
        echo "<td>${Marvisitors}</td>"
        echo "</tr>"
        echo "<td>April</td>"
        echo "<td>${Aprvisitors}</td>"
        echo "</tr>"
        echo "<td>May</td>"
        echo "<td>${Mayvisitors}</td>"
        echo "</tr>"
        echo "<td>June</td>"
        echo "<td>${Junvisitors}</td>"
        echo "</tr>"
        echo "<td>July</td>"
        echo "<td>${Julvisitors}</td>"
        echo "</tr>"
        echo "<td>August</td>"
        echo "<td>${Augvisitors}</td>"
        echo "</tr>"
        echo "<td>September</td>"
        echo "<td>${Sepvisitors}</td>"
        echo "</tr>"
        echo "<td>October</td>"
        echo "<td>${Octvisitors}</td>"
        echo "</tr>"
        echo "<td>November</td>"
        echo "<td>${Novvisitors}</td>"
        echo "</tr>"
        echo "<td>December</td>"
        echo "<td>${Decvisitors}</td>"
        echo "</tr>"
        echo "<tr>"
        echo "<td>Total Vivistor:</td>"
        echo "<td>$totalvisitors</td>"
        echo "</tr>"
        echo "</table>"
        echo "</body>"
        echo "</html>"
} > Myprogram3.html

echo "Loading data into web static is successful!!!"
}


while [ -n "$1" ] ; do
  case $1 in
     -h*|-H*)        help ; #echo "help msg1" ; echo "help msg2" ;
                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
#  echo "Got month : $the_month"
#  echo "Got year: $the_year"
#countVisitors
genWebpage

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
 楼主| 发表于 2007-4-26 15:43:21 | 显示全部楼层
...................
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表