shell / pyhton脚本处理时间
--------------------------------
#环境 beta、dev、pre、pro
BDPP="`cat ${ads}/config/args.properties|grep "hdfs_dir="|awk -F "_" '{print$NF}' `"
#环境 beta、dev、pre、pro
BDPP="`cat ${ads}/config/args.properties|grep "hive_db_ex="|awk -F "_" '{print$NF}' `"
#检查环境[shell写法]
if [ $BDPP == "beta" ]; then
BDPP_s="beta"
elif [ $BDPP == "pre" ]; then
BDPP_s="pre"
elif [ $BDPP == "dev" ]; then
BDPP_s="dev"
elif [ $BDPP == "bi" ]; then
BDPP_s="pro"
else:
echo "公有配置参数(${BDPP_s})不在范围内,请核对后再执行...退出程序!!!"
fi
-----------------------
# 内部表hive数据库:[pro(默认为空)/dev/beta/pre][shell写法]
BDPP="`cat ${adds}/config/args.properties |grep "hive_db_in=" |awk -F"_" '{print$NF}'`"
echo "BDPP=$BDPP"
#检查环境[环境+内部表]
if [ $BDPP != "bi" ]; then
ODSDB="appdata_bi_${BDPP}"
elif [ $BDPP == "bi" ]; then
ODSDB="appdata_bi"
BDPP="pro"
fi
----------------------- shell中执行
# 内部表hive数据库:[pro(默认为空)/dev/beta/pre]
BDPP="`cat ${adds}/config/args.properties |grep "hive_db_app=" |awk -F"_" '{print$NF}'`"
#报表库
appDB="`cat ${adds}/config/args.properties |grep "hive_db_app=" |awk -F"=" '{print$2}'`"
hostname="`hostname`"
#检查环境[环境+内部表]
if [ $BDPP != "bi" ]; then
echo "测试环境[${hostname}]:BDPP=${BDPP}" #环境
elif [ $BDPP == "bi" ]; then
BDPP="pro"
echo "生产环境[${hostname}]:BDPP=${BDPP}" #环境
fi
-----------------------
# 检查环境[python写法]
if [ $BDPP == "bi" ]; then
BDPP="pro"
echo ">>> 检查公有配置环境:${BDPP} 外部表:${sdb} 内部表:${db}"
elif [ $BDPP != "bi" ]; then
echo ">>> 检查公有配置环境:${BDPP} 外部表:${sdb} 内部表:${db}"
else :
echo "${BDPP}不在范围内,请确定后再执行,退出...."
fi
## 调用对应基础库配置环境[pro(默认为空)/dev/beta/pre]
hdfsdir = common_properties["hdfs_dir"]
if "dev" in hdfsdir:
sdb = "dev"
elif "beta" in hdfsdir:
sdb = "beta"
elif "pre" in hdfsdir:
sdb = "pre"
else:
sdb = "pro"
-----------------------------报表计算
#提取运行脚本后缀携带时间参数,没携带就取共有配置文件时间,默认是昨天 写法 2018-01-10/20180110
if len(sys.argv) == 2:
run_day = sys.argv[1]
run_day = (run_day.replace("-",""))[0:8]
print "自携带运行时间:",run_day
elif len(sys.argv) == 1:
run_day = common_properties["run_day"]
if run_day == "" or run_day == "9999-99-99":
today = date.today()
oneday = timedelta(days=1)
yesterday = today - oneday
run_day = str(yesterday).replace("-", "")
print "公有配置9999-99-99,选取默认昨天为运行时间:",run_day
elif run_day != "9999-99-99":
run_day = (run_day.replace("-", ""))[0:8]
print "公有配置9999-99-99,被指定运行时间:",run_day
## 调用对应基础库配置环境[pro(默认为空)/dev/beta/pre]
if "dev" in db_in:
db = "dev"
elif "beta" in db_in:
db = "beta"
elif "pre" in db_in:
db = "pre"
else:
db = "pro"
print "当前执行计算报表环境:",db
-----------------------------报表同步数据
#日志前缀
pre_log = common_properties["logdirs"]
#提取运行脚本后缀携带时间参数,没携带就取共有配置文件时间,默认是昨天 写法 2018-01-10/20180110
if len(sys.argv) == 2:
run_day = sys.argv[1]
run_day = (run_day.replace("-",""))[0:8]
print "自携带运行时间:",run_day
elif len(sys.argv) == 1:
run_day = common_properties["run_day"]
if run_day == "" or run_day == "9999-99-99":
today = date.today()
oneday = timedelta(days=1)
yesterday = today - oneday
run_day = str(yesterday).replace("-", "")
print "公有配置9999-99-99,选取默认昨天为运行时间:",run_day
elif run_day != "9999-99-99":
run_day = (run_day.replace("-", ""))[0:8]
print "公有配置9999-99-99,被指定运行时间:",run_day
## 调用对应基础库配置环境[pro(默认为空)/dev/beta/pre]
if "dev" in db_app:
db = "dev"
elif "beta" in db_app:
db = "beta"
elif "pre" in db_app:
db = "pre"
else:
db = "pro"
print "当前执行计算报表环境:",db
开始时间
或者
## 调用对应基础库配置环境[pro(默认为空)/dev/beta/pre]
if db_app.endswith("beta"):
db = "beta"
elif db_app.endswith("dev"):
db = "dev"
elif db_app.endswith("pre"):
db = "pre"
elif db_app.endswith("bi"):
db = "pro"
语法
endswith()方法语法:
str.endswith(suffix[, start[, end]])
参数
suffix -- 该参数可以是一个字符串或者是一个元素。
start -- 字符串中的开始位置。
end -- 字符中结束位置。
--------------------------------------
echo -n "Please type[y/n/yes/no]: "
read input
#1. 先转换小写到大写,再通过cut截取第一个字符。
ret=`echo $input | tr '[a-z]' '[A-Z]' | cut -c1`
if [ $ret = "Y" ]; then
echo "Your input is Y."
elif [ $ret = "N" ]; then
echo "Your input is N."
else
echo "Your input is error."
fi------------------------------
if hdfs_dir.endswith("beta"):
db = "beta"
elif hdfs_dir.endswith("dev"):
db = "dev"
elif hdfs_dir.endswith("pre"):
db = "pre"
elif hdfs_dir.endswith("bi"):
db = "pro"
====================================================================================================
#提取运行脚本后缀携带时间参数,没携带就取共有配置文件时间,默认是昨天 写法 2018-01-10/20180110
if len(sys.argv) == 2:
run_day = sys.argv[1].replace("-","")[0:8]
dtimes = datetime.strptime(run_day, '%Y%m%d') #字符串换日期[dtimes= 2018-07-11 00:00:00]
nextday = dtimes + timedelta(days=1)
nextday = str(nextday).replace("-","")[0:8]
run_day = run_day.replace("-","")[0:8]
print "自携带运行时间:%s ,运行时间:%s ,nextday[自增一天]:%s "%(sys.argv[1],run_day,nextday)
elif len(sys.argv) == 1:
run_day = common_properties["run_day"]
if run_day == "" or run_day == "9999-99-99":
today = datetime.today()
oneday = timedelta(days=1)
yesterday = today - oneday
run_day = str(yesterday).replace("-", "")[0:8]
nextday = str(today).replace("-", "")[0:8]
print "公有配置9999-99-99,选取默认昨天为运行时间:%s, nextday[自增一天]:%s "%(run_day,nextday)
elif run_day != "9999-99-99":
run_day1 = run_day.replace("-","")[0:8]
dtimes = datetime.strptime(run_day1, '%Y%m%d')
nextday = dtimes + timedelta(days=1)
nextday =str(nextday).replace("-", "")[0:8]
print "公有配置9999-99-99,被指定运行时间:%s, nextday[自增一天]:%s "%(run_day,nextday)
run_day = str(run_day1.replace("-", ""))[0:8]
=====================================明天后天今天======================================
#提取运行脚本后缀携带时间参数,没携带就取共有配置文件时间,默认是昨天 写法 2018-01-10/20180110
if len(sys.argv) == 2:
run_day = sys.argv[1].replace("-","")[0:8]
dtimes = datetime.strptime(run_day, '%Y%m%d') #字符串换日期[dtimes= 2018-07-11 00:00:00]
nextday = dtimes + timedelta(days=1)
yesterday = dtimes + timedelta(days = -1)
yesterday = str(yesterday).replace("-", "")[0:8]
nextday = str(nextday).replace("-","")[0:8]
run_day = run_day.replace("-","")[0:8]
print "自携带运行时间:%s ,运行时间:%s ,nextday[自增一天]:%s yesterday[减去一天]:%s"%(sys.argv[1],run_day,nextday,yesterday)
elif len(sys.argv) == 1:
run_day = common_properties["run_day"]
if run_day == "" or run_day == "9999-99-99":
today = datetime.today()
oneday = timedelta(days=1)
run_day1 = today - oneday
yesterday1 = run_day1 + timedelta(days = -1)
run_day = str(run_day1).replace("-", "")[0:8]
yesterday = str(yesterday1).replace("-", "")[0:8]
nextday = str(today).replace("-", "")[0:8]
print "公有配置9999-99-99,选取默认昨天为运行时间:%s, nextday[自增一天]:%s, yesterday[减去一天]:%s "%(run_day,nextday,yesterday)
elif run_day != "9999-99-99":
run_day1 = run_day.replace("-","")[0:8]
dtimes = datetime.strptime(run_day1, '%Y%m%d')
nextday = dtimes + timedelta(days=1)
yesterday1 = dtimes + timedelta(days = -1)
yesterday = str(yesterday1).replace("-", "")[0:8]
nextday =str(nextday).replace("-", "")[0:8]
print "公有配置9999-99-99,被指定运行时间:%s, nextday[自增一天]:%s ,yesterday[减去一天]:%s "%(run_day,nextday,yesterday)
run_day = str(run_day1.replace("-", ""))[0:8]
print "run_day=",run_day