今天写的shell脚本,主要是对输入参数检验
usage()
{
echo "\nUSAGE:\n"
echo "PmActivityReport.sh\t-type\t<latency|activity>\t\t\t(Mandatory)\t/*Report type.*/\n"
echo "\t\t\t-ne\t<RNC-IN|RNC-AN|RNC-CN|BTS|ALL>\t\t(Optional)\t/*Ne type.*/\n"
echo "\t\t\t-from\t<YYYY/MM/DD-HH:00>\t\t\t(Optional)\t/*Begin time.*/\n"
echo "\t\t\t-to\t<YYYY/MM/DD-HH:00>\t\t\t(Optional)\t/*End time.*/\n"
echo "\t\t\t-o\t<>\t\t\t\t\t(Optional)\t/*Output file name.*/\n"
echo "\nNOTE:\n"
echo "The default value of the ne type is \"ALL\"."
echo "The default value of the output file name is \"latencyReport.txt\" or \"activityReport.txt\", depending on the report type."
echo "Time must be given in a strict format: YYYY/MM/DD-HH:MM. Only full time is allowed, and the minutes can not be set different from \"00\".\n"
}
if [ $# -lt 2 ]
then
usage
else
reportType="UNKNOWN"
neType="ALL"
fromTime="UNKNOWN"
toTime="UNKNOWN"
output="UNKNOWN"
totalPara=$#
para=1
paraIsValid=1
while [ $para -le $totalPara ]
do
case "$1" in
-type)
if [ "$2" = "latency" -o "$2" = "activity" ]
then
reportType=$2
else
echo "Wrong parameter for -type!"
paraIsValid=0
fi
;;
-ne)
if [ "$2" = "RNC-IN" -o "$2" = "RNC-AN" -o "$2" = "RNC-CN" -o "$2" = "BTS" -o "$2" = "ALL" ]
then
neType=$2
else
echo "Wrong parameter for -ne!"
paraIsValid=0
fi
;;
-from)
timeIsValid=`echo $2 | grep -c [0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]-[0-9][0-9]:00$`
if [ $timeIsValid -eq 1 ]
then
Year=`echo $2 | cut -c1-4`
Month=`echo $2 | cut -c6-7`
Day=`echo $2 | cut -c9-10`
Hour=`echo $2 | cut -c12-13`
if [ $Month -ge 13 ]
then
echo "Wrong parameter for -from! Please check the month."
timeIsValid=0
fi
if [ $Day -ge 32 ]
then
echo "Wrong parameter for -from! Please check the day."
timeIsValid=0
fi
if [ $Hour -ge 25 ]
then
echo "Wrong parameter for -from! Please check the hour."
timeIsValid=0
fi
if [ $timeIsValid = 1 ]
then
timeFrom=$2
else
paraIsValid=0
fi
else
echo "Wrong parameter format for -from!"
paraIsValid=0
fi
;;
-to)
timeIsValid=`echo $2 | grep -c [0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]-[0-9][0-9]:00$`
if [ $timeIsValid -eq 1 ]
then
Year=`echo $2 | cut -c1-4`
Month=`echo $2 | cut -c6-7`
Day=`echo $2 | cut -c9-10`
Hour=`echo $2 | cut -c12-13`
if [ $Month -ge 13 ]
then
echo "Wrong parameter for -to! Please check the month."
timeIsValid=0
fi
if [ $Day -ge 32 ]
then
echo "Wrong parameter for -to! Please check the day."
timeIsValid=0
fi
if [ $Hour -ge 25 ]
then
echo "Wrong parameter for -to! Please check the hour."
timeIsValid=0
fi
fromTimeValue=`echo $timeFrom | sed -e "s/\///g" | sed -e "s/-//" | sed -e "s/://"`
toTimeValue=`echo $2 | sed -e "s/\///g" | sed -e "s/-//" | sed -e "s/://"`
if [ $toTimeValue -le $fromTimeValue ]
then
echo "Wrong parameter for -to! The time for -to is not later than that for -from."
timeIsValid=0
fi
if [ $timeIsValid = 1 ]
then
timeTo=$2
else
paraIsValid=0
fi
else
echo "Wrong parameter format for -to!"
paraIsValid=0
fi
;;
-o)
if [ "$2" != "" ]
then
output=$2
else
echo "Wrong parameter for -o!"
paraIsValid=0
fi
;;
*)
echo "Bad option \"$1\"."
paraIsValid=0
;;
esac
para=`expr $para + 2`
shift
if [ $# -gt 1 ]
then
shift
fi
done
if [ $paraIsValid -eq 0 -o $reportType = "UNKNOWN" ]
then
usage
else
if [ $reportType = "latency" ]
then
output="latencyReport.txt"
fi
if [ $reportType = "activity" ]
then
output="activityReport.txt"
fi
fi
fi