最近好多需要用shell脚本做的工作


日期区间循环模板

支持年月日小时为基本单位的递增循环,具体时间格式自己再使用date命令转化一下就行了,日期操作最精简的模板。

dateStart=$2
dateEnd=$3

echo "start:"$dateStart
echo "end:"$dateEnd

#year month day hour
step=$1


if [ $step == "hour" ]
then
    dateFormat='+%Y-%m-%d %H:%M:%S'
else
    dateFormat='+%Y%m%d'
fi

dateStart=`date -d "-1 ${step} ${dateStart}" "${dateFormat}"`

#足够长的循环,依赖dateEnd跳出
for i in {1..10000}
do
    dateNow=`date -d "+$i ${step} ${dateStart}" "${dateFormat}"`
    #结束时间
    if [ "$dateNow" \> "$dateEnd" ]
    then
        break
    fi
    #业务操作
    echo $dateNow

    #中途跳出 关键业务人工确认,血泪教训
    #read -p "can continue?(y/n):" flag
    #if [ $flag != "y" ]
    #then
    #    break
    #fi
done

使用方法:

>sh data_range_tem.sh hour '2020-01-01 00:00:00' '2020-01-01 12:00:00'
start:2019-12-31 23:00:00
end:2020-01-01 12:00:00
2020-01-01 00:00:00
2020-01-01 01:00:00
2020-01-01 02:00:00
2020-01-01 03:00:00
2020-01-01 04:00:00
2020-01-01 05:00:00
2020-01-01 06:00:00
2020-01-01 07:00:00
2020-01-01 08:00:00
2020-01-01 09:00:00
2020-01-01 10:00:00
2020-01-01 11:00:00
2020-01-01 12:00:00
>sh data_range_tem.sh day 20200701 20200712
start:20200630
end:20200712
20200701
20200702
20200703
20200704
20200705
20200706
20200707
20200708
20200709
20200710
20200711
20200712
[work@cashdesk-eason script_2]$ sh data_range_tem.sh year 20600701 20770701 
start:20590701
end:20770701
20600701
20610701
20620701
20630701
20640701
20650701
20660701
20670701
20680701
20690701
20700701
20710701
20720701
20730701
20740701
20750701
20760701
20770701

用上面的模板改造了一个迁移greenplum数据的脚本(gp是真的太难用了)
执行效果

2019-01-01--2019-02-01
INSERT 0 42884230
can continue?(y/n):y
DELETE 42884230

2019-02-01--2019-03-01
INSERT 0 38915294
can continue?(y/n):y
DELETE 38915294

2019-03-01--2019-04-01
INSERT 0 43684987
can continue?(y/n):y
DELETE 43684987

2019-04-01--2019-05-01
INSERT 0 49299915
can continue?(y/n):y
DELETE 49299915

读取管道命令

test.args

1 3 4 1,2,3 qwe
ao li gei

showArgv.sh

echo "$*"
count=1
for param in "$@"
do
    echo "param #$count = $param"
    count=$[ $count + 1]
done

readPipline.sh

while read file
do
    echo $file
done
>cat test.args | xargs ./showArgv.sh
1 3 4 1,2,3 qwe ao li gei
param #1 = 1
param #2 = 3
param #3 = 4
param #4 = 1,2,3
param #5 = qwe
param #6 = ao
param #7 = li
param #8 = gei
>cat test.args | ./readPipline.sh
1 3 4 1,2,3 qwe
ao li gei
最后修改:2021 年 01 月 12 日
如果觉得我的文章对你有用,请随意赞赏