今天写的shell脚本,主要是对输入参数检验
2004-10-29 21:18
393 查看
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
{
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
相关文章推荐
- Shell脚本中判断输入参数个数的方法
- Shell脚本中判断输入参数个数的方法
- Shell脚本中判断输入参数个数的方法
- shell脚本之判断输入参数是否为整数值的实例
- Shell脚本中判断输入变量或者参数是否为空的方法
- 验证用户输入的参数合法性的shell脚本
- 验证用户输入的参数合法性的shell脚本
- shell脚本传参实现scp命令无需输入密码和别的参数
- 脚本获取输入参数shell之getopt
- LINUX下用SHELL脚本执行带输入输出参数的ORACLE存储过程并得到结果
- Shell脚本中判断输入变量或者参数是否为空的方法
- shell脚本中用getopts处理用户输入选项和参数
- shell脚本判断输入参数个数
- Shell脚本中判断输入参数个数的方法
- Shell脚本中判断输入参数个数的方法投稿:junjie 字体:[增加 减小] 类型:转载
- Shell脚本中判断输入参数个数的方法 ,包括默认的$0,$1,-d ,-eq -n -z 等逻辑判断选项
- Shell 编程学习笔记:脚本输入参数
- Linux_Shell_脚本参数接收键盘输入
- shell脚本之判断输入参数是否为整数值
- shell学习 - 处理脚本的多参数输入