- 论坛徽章:
- 0
|
本帖最后由 fufq 于 2010-02-12 14:57 编辑
脚本内容如下:
root@# more offlinereason.sh
#!/bin/ksh
YEAR=`date +%Y`;
date1=`date +%d`; #当前日期,用以判断是否为每月1日
MONTH=`date +%m`;
yesterday1=`TZ=$TZ+32 date +%Y%m%d`; #昨天的日期,格式:20100210,查询条件
yesterday2=`TZ=$TZ+32 date +%Y_%m_%d`;#昨天的日期,格式:2010_02_10,用做导出的文件名
echo "1-Year is "$YEAR " MONTH IS "$MONTH ", yes1 is "$yesterday1>offlinereason.sql
if [ $date1 -eq 01 ]; then #当前日期为01日,则话单表月份需取上个月月份
{
if [ $MONTH -eq 01 ]; then #当前月份为01月份,则需要获取到去年12月份的值,否则保持当前值不变
{
MONTH=12
YEAR=`expr $YEAR - 1`
echo "date is 01,and month is 01,MONTH-1 IS "$MONTH" ,YEAR is "$YEAR>>offlinereason.sql
};else #非1月份,则当日期为01日的时候,月份直接取上一月月份,年不变;
{
MONTH=`expr $MONTH - 1`
echo "date is 01,month is not 01,than month-1 is "$MONTH>>offlinereason.sql
} ;fi
};
fi #如果当前日期不为01,则,月份保持为当月不变
echo "unload to $yesterday2.txt
select * from rec_210_$YEAR$MONTH where substr(begintime,1, ='$yesterday1'">>offlinereason.sql
==========================================
当前系统日期:
root@# date
2010年01月01日 星期五 14时41分14秒 CST
==========================================
脚本执行结果如下:
root@CNCSCDN-SHPTL1 # more offlinereason.sql
1-Year is 2010 MONTH IS 01 , yes1 is 20091230
date is 01,and month is 01,MONTH-1 IS 12 ,YEAR is 2009
unload to 2009_12_30.txt
select * from rec_210_200912 where substr(begintime,1, ='20091230'
+================================
问题:
1、为何每月01日的话,yesterday取到的前一天日期都是上月的30日,永远取不到31日?
2、同1,怎么才可以按不同的月份取到前一天日期实际的30日、31日或者28日?
3、当当前月份为02月时,最后结果MONTH-1=1了,而不是我要的01,如:
root@# date
2010年02月01日 星期一 14时30分20秒 CST
执行结果:
root@ # more offlinereason.sql
1-Year is 2010 MONTH IS 02 , yes1 is 20100130
date is 01,month is not 01,than month-1 is 1
unload to 2010_01_30.txt
select * from rec_210_20101 where substr(begintime,1, ='20100130' |
|