循环脚本

来源:互联网 发布:stm32单片机教程 pdf 编辑:程序博客网 时间:2024/06/08 02:35
声明:允许转载,转载请注明链接,谢谢合作!
很多时候我们在执行sh命令需要执行一个时间段的脚本任务,循环调用是很常见的,以下的脚本用于递归执行一个时间段的脚本。
代码如下:
#!/bin/sh
#circle_operate.sh    脚本名称
#run shell script between two date
#程序递归脚本
#OPERATE_DATE        操作数据日期
#author:wangxin   version:1.0 
#create_date:20160202 
##################################################################
#check the input param num is correct
#判断入参个数是否正确
#check the first param is not empty
if [ -z "$1" ]; then
echo "param1 error "
echo "you need to input a pre_commond"
echo "the correct formate is : ./circle_operate.sh  commond1start_date end_date commond2"
exit 
fi
#check the second param is not empty
if [ -z "$2" ]; then
echo "param2 error "
echo "you need to input a start date"
echo "the correct formate is : ./circle_operate.sh  commond1start_date end_date commond2"
exit 
fi
#check the third param is not empty
if [ -z "$3" ];then
echo "param3 error "
echo "you need to input a end date"
echo "the correct formate is : ./circle_operate.sh  commond1start_date end_date commond2"
exit 
fi
#check the fourth param is not empty
if [ -z "$4" ];then
echo "param4 error "
echo "you need to input a end_commond"
echo "the correct formate is : ./circle_operate.sh  commond1start_date end_date commond2"
exit    
fi
#
#
echo "program start"
#Achieve first commond 
#获取前部分命令
begin_commond=$1
echo "begin_commond is $begin_commond"
#
#Achieve start_date
#获取开始日期
begin_date=$2
echo "begin_date is $begin_date"
#
#Achieve end_date
#获取结束日期
end_date=$3
echo "end_date is $end_date"
#
#Achieve start_date
#获取后部分命令
end_commond=$4
echo "end_commond is $end_commond"
#
#mark the last_date
#明确结束日期
last_date=`date -d $end_date' next-day' '+%Y%m%d' `
echo "the script will exit before $last_date"
#
#set operate_date
#给运行日期赋值
operate_date=$begin_date
#
while [ $operate_date -lt $last_date ]
do
echo "the current operate_date is $operate_date"
echo "$begin_commond $operate_date $end_commond"
operate_date=`date -d $operate_date' next-day' '+%Y%m%d'`
done
#
echo "program exit"