shell设置时间递减脚本
经常要用shell来做时间的定时任务,尤其是用sqoop脚本拉取数据的时候,那么假如当你要导入数据是残缺的时候呢,我写了一个能自定义时间并逐条递减的程序
#!/bin/bash
. /etc/profile
. ~/.bash_profile
set -x
cd $(dirname $0)
#ttt=259
for wl in $( seq 1 300); do
#statements
DATE1=$(date -d "${wl} day ago 20170725" +%Y-%m-%d) #20170725的前一天 遍历减一天
DATE2=$(date -d "$(($wl-1)) day ago 20170725" +%Y-%m-%d) #20170725这一天 遍历当天
dt=${DATE1//-/} #设置时间格式用于分区
done
如果想获得当前时间递减就把 ago 20170725去掉就可以了
运行命令: bash -test1.sh
运行结果:
++ export PATH
+ set -x
++ dirname test1.sh
+ cd .
+ HADOOP_BIN=/workspace/carkey/hadoop/latest/bin/hadoop
+ HIVE_BIN=/workspace/carkey/apache-hive-2.1.1-bin/bin/hive
+ SQOOP_BIN=/workspace/carkey/sqoop-1.4.6.bin__hadoop-2.0.4-alpha/bin/sqoop
++ seq 1 100
+ for ijk in '$( seq 1 100)'
++ date -d '1 day ago 20170725' +%Y-%m-%d
+ DATE1=2017-07-24
++ date -d '0 day ago 20170725' +%Y-%m-%d
+ DATE2=2017-07-25
+ dt=20170724
+ for ijk in '$( seq 1 100)'
++ date -d '2 day ago 20170725' +%Y-%m-%d
+ DATE1=2017-07-23
++ date -d '1 day ago 20170725' +%Y-%m-%d
+ DATE2=2017-07-24
+ dt=20170723
+ for ijk in '$( seq 1 100)'
++ date -d '3 day ago 20170725' +%Y-%m-%d
+ DATE1=2017-07-22
++ date -d '2 day ago 20170725' +%Y-%m-%d
+ DATE2=2017-07-23
+ dt=20170722
+ for ijk in '$( seq 1 100)'
++ date -d '4 day ago 20170725' +%Y-%m-%d
+ DATE1=2017-07-21
++ date -d '3 day ago 20170725' +%Y-%m-%d
+ DATE2=2017-07-22
+ dt=20170721
+ for ijk in '$( seq 1 100)'
++ date -d '5 day ago 20170725' +%Y-%m-%d
+ DATE1=2017-07-20
++ date -d '4 day ago 20170725' +%Y-%m-%d
+ DATE2=2017-07-21
+ dt=20170720
+ for ijk in '$( seq 1 100)'
++ date -d '6 day ago 20170725' +%Y-%m-%d
+ DATE1=2017-07-19
++ date -d '5 day ago 20170725' +%Y-%m-%d
+ DATE2=2017-07-20
+ dt=20170719
+ for ijk in '$( seq 1 100)'
++ date -d '7 day ago 20170725' +%Y-%m-%d
+ DATE1=2017-07-18
++ date -d '6 day ago 20170725' +%Y-%m-%d
+ DATE2=2017-07-19
+ dt=20170718
+ for ijk in '$( seq 1 100)'
++ date -d '8 day ago 20170725' +%Y-%m-%d
+ DATE1=2017-07-17
++ date -d '7 day ago 20170725' +%Y-%m-%d
+ DATE2=2017-07-18
+ dt=20170717
+ for ijk in '$( seq 1 100)'
++ date -d '9 day ago 20170725' +%Y-%m-%d
+ DATE1=2017-07-16
++ date -d '8 day ago 20170725' +%Y-%m-%d
+ DATE2=2017-07-17
+ dt=20170716
+ for ijk in '$( seq 1 100)'
++ date -d '10 day ago 20170725' +%Y-%m-%d
+ DATE1=2017-07-15
++ date -d '9 day ago 20170725' +%Y-%m-%d
+ DATE2=2017-07-16
+ dt=20170715
+ for ijk in '$( seq 1 100)'
++ date -d '11 day ago 20170725' +%Y-%m-%d
+ DATE1=2017-07-14
++ date -d '10 day ago 20170725' +%Y-%m-%d
+ DATE2=2017-07-15
+ dt=20170714
+ for ijk in '$( seq 1 100)'
++ date -d '12 day ago 20170725' +%Y-%m-%d
+ DATE1=2017-07-13
++ date -d '11 day ago 20170725' +%Y-%m-%d
+ DATE2=2017-07-14
+ dt=20170713
+ for ijk in '$( seq 1 100)'
++ date -d '13 day ago 20170725' +%Y-%m-%d
+ DATE1=2017-07-12
++ date -d '12 day ago 20170725' +%Y-%m-%d
+ DATE2=2017-07-13
+ dt=20170712
+ for ijk in '$( seq 1 100)'
++ date -d '14 day ago 20170725' +%Y-%m-%d
+ DATE1=2017-07-11
++ date -d '13 day ago 20170725' +%Y-%m-%d
+ DATE2=2017-07-12
+ dt=20170711
+ for ijk in '$( seq 1 100)'
++ date -d '15 day ago 20170725' +%Y-%m-%d
+ DATE1=2017-07-10
++ date -d '14 day ago 20170725' +%Y-%m-%d
+ DATE2=2017-07-11
+ dt=20170710