循环脚本
声明:允许转载,转载请注明链接,谢谢合作!
很多时候我们在执行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
commond1 start_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
commond1 start_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
commond1 start_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
commond1 start_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"
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端