在shell开发中,会遇到将10以内的数字前面加0的情况,例如按小时分区执行hive脚本,具体代码如下:
#!/bin/sh
START_DAY=$1
END_DAY=$2
FLAG_DAY=${START_DAY}
START_HOUR=$3
END_HOUR=$4
executeHive() {
while :
do
local STAT_HOUR=00
local FLAG_HOUR=23
local START_DAY=$(date -d "$START_DAY 1day" +%Y%m%d)
local STAT_DAY=`date -d "${START_DAY} -1 day" +%Y%m%d`
# 判断执行日期是否为开始日期或结束日期
if [ ${STAT_DAY} -eq ${FLAG_DAY} ]; then
STAT_HOUR=${START_HOUR}
elif [ ${STAT_DAY} -eq ${END_DAY} ]; then
FLAG_HOUR=${END_HOUR}
fi
while :
do
printf "[INFO] `date '+%F %T'` current execute partition is dt=${STAT_DAY} hour=${STAT_HOUR}\n"
exitCode=$?
if [ $exitCode -ne 0 ];then
printf "[ERROR] `date '+%F %T'` hivesql execute ${SOL_FILE} is failed!!!\n"
exit $exitCode
else
printf "[INFO] `date '+%F %T'` hivesql ${STAT_DAY} ${STAT_HOUR} data execute success!!!\n"
fi
# 因为小时小于10时前面有0,shell脚本会认为是八进制数字,所以要转为十进制数字
#STAT_HOUR=$((10#$STAT_HOUR+1))
STAT_HOUR=$(($STAT_HOUR+1))
# 判断小时是否小于10
if [ ${STAT_HOUR} -lt 10 ]; then
STAT_HOUR=0${STAT_HOUR}
fi
# 如果执行小时大于设置的结束小时,退出循环
if [ ${STAT_HOUR} -gt ${FLAG_HOUR} ]; then
break;
fi
done
# 判断执行日期是否等于结束日期,等于则退出循环
if [ ${STAT_DAY} -eq ${END_DAY} ]; then
break;
fi
done
}
if [ ${START_DAY} -gt ${END_DAY} ]; then
printf "[ERROR] `date '+%F %T'` --start-day:${START_DAY} greater than --end-day:${END_DAY}\n"
exit 1
elif [ ${START_HOUR} -ge 24 ] || [ ${START_HOUR} -lt 0 ] || [ ${END_HOUR} -ge 24 ] || [ ${END_HOUR} -lt 0 ]; then
printf "[ERROR] `date '+%F %T'` --start-hour:${START_DAY} or --end-hour:${END_DAY} is not 00-23 \n"
exit 1
else
executeHive
printf "[INFO] `date '+%F %T'` hivesql ${START_DAY} to ${END_DAY} data execute success!!!\n"
fi
当执行命令:sh houeTest.sh 20200202 20200203 19 23
会报如下错误:houeTest.sh: line 38: 08: value too great for base (error token is "08")
解决方法:将代码第38行改为第37行STAT_HOUR=$(($STAT_HOUR+1))
改为:STAT_HOUR=$((10#$STAT_HOUR+1))
就不会报错了
来源:CSDN
作者:浮云6363
链接:https://blog.csdn.net/lz6363/article/details/104186670