shell按月份循环执行hive脚本

在开发中,怎样编写脚本输入开始月份、结束月份自动跑数呢?

对shell中getopts不懂的请查看我以前博客
shell中的getopt

提供以下脚本供参考:

#!/bin/sh
#*****************************************************************************************
#*** 程序功能:  按天循环导入分区数据
#*** 输入参数:  -s  :需要导入的开始月份,格式YYYYMM
#****           -e  :需要导入的结束月份,格式YYYYMM
#****           -f :SQL脚本绝对路径名称
#*** 编 写 人:  fuyun
#*** 编写日期:  2020-02-18
#*** 修 改 人:  
#*****************************************************************************************

#若指令传回值不等于0,则立即退出shell
#set -e
umask 002


while getopts s:e:f: OPT; do #选项后面的冒号表示该选项需要参数
  case ${OPT} in
    s) START_MONTH=${OPTARG}
       ;;
    e) END_MONTH=${OPTARG}
       ;;
    f) SOL_FILE=${OPTARG}
       ;;
    \?)
       printf "[Usage] `date '+%F %T'` `basename $0` -s  -e  -f \n" >&2
       exit 1
  esac
done

# check parameter
if [ -z "${START_MONTH}" -o -z "${END_MONTH}" -o -z "${SOL_FILE}" ]; then
    printf "[ERROR] `date '+%F %T'` following parameters is empty:\nSTART_MONTH=${START_MONTH}\nEND_MONTH=${END_MONTH}\n"
    printf "SOL_FILE=${SOL_FILE}\n"
    exit 1
fi

LOG_FILE=$(echo "${SOL_FILE}" | awk -F"/" '{print $NF}' | awk -F"." '{print $1}')
MONTHS=`seq $START_MONTH $END_MONTH`

printf "[INFO] `date '+%F %T'` execute start day is ${START_MONTH}\n"
printf "[INFO] `date '+%F %T'` execute end day is ${END_MONTH}\n"
printf "[INFO] `date '+%F %T'` execute SQL file is ${SOL_FILE}\n"
printf "[INFO] `date '+%F %T'` looping execute.......\n"

executeHive() {
  for STAT_MONTH in $MONTHS
  do  
    if [ ${STAT_MONTH: 4: 2} -le "12" ] && [ ${STAT_MONTH: 4: 2} -ne "00" ]; then
      local LOG_PATH=/home/fuyun/logs/${LOG_FILE}.log
      printf "[INFO] `date '+%F %T'` log file name is ${LOG_PATH}\n"
      printf "[INFO] `date '+%F %T'` current execute partition is ${STAT_MONTH}\n"
      local startTime=`date '+%F %T'`
      local startSeconds=$(date --date="${startTime}" +%s)
      printf "[INFO] `date '+%F %T'` hivesql execute start time is ${startTime}\n"
      
      # execute sql
      hive --hiveconf MONTH=${STAT_MONTH} -f ${SOL_FILE}>${LOG_PATH}.tmp 2>${LOG_PATH}
      
      exitCode=$?
      if [ $exitCode -ne 0 ];then
        printf "[ERROR] `date '+%F %T'` hivesql execute ${SOL_FILE} is failed!!!\n"
        exit $exitCode
      else
        local endTime=`date '+%F %T'`
        local endSeconds=$(date --date="${endTime}" +%s)
        printf "[INFO] `date '+%F %T'` hivesql execute end time is ${endTime}\n"
        printf "[INFO] `date '+%F %T'` hivesql execute time is $(($((endSeconds-startSeconds))/60)) minutes\n"
        printf "[INFO] `date '+%F %T'` hivesql ${STAT_MONTH} data execute success!!!\n"
        printf "[INFO] `date '+%F %T'` delete temp log ${LOG_PATH}.tmp\n"
        rm ${LOG_PATH}.tmp
      fi
    fi
  done 
}

if [ ${START_MONTH} -gt ${END_MONTH} ]; then
  printf "[ERROR] `date '+%F %T'` -s:${START_MONTH} greater than -e:${END_MONTH}\n"
else
  executeHive
  printf "[INFO] `date '+%F %T'` hivesql ${START_MONTH} to ${END_MONTH} data execute success!!!\n"
fi

你可能感兴趣的:(shell)