if逻辑判断脚本+参数检验

if的逻辑关系判断脚本

1) bash
   a=3 ; b=2 ; c=4
   if (( a > b )) && (( a < c ))
 
   或者

   if [[ $a > $b ]] && [[ $a < $c ]]

    或者
   
     if [ $a -gt $b -a $a -lt $c ]

2)   a=3 ; b=2 ; c=4

      if (( a > b )) || (( a < c ))
     
       或者
      if [[ $a > $b ]] || [[ $a < $c ]]

       或者
       if [ $a -gt $b -o $a -lt $c ]

 

 

 

输入参数检验

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值