我如何编写一个shell脚本,从开始日期到当前日期每天调用另一个脚本



如何编写一个shell脚本,每天调用另一个脚本

。E当前有

#!/bin/sh
./update.sh 2020 02 01
./update.sh 2020 02 02
./update.sh 2020 02 03

但我只想指定开始日期(2020 02 01),并让每天运行update.sh到当前日期,但不知道如何在shell脚本中操作日期。

我尝试了一下,但相当混乱,希望它能自己处理日期。

#!/bin/bash
for j in {4..9}
do
for k in {1..9}
do
echo "update.sh" 2020 0$j 0$k
./update.sh 2020 0$j 0$k
done
done
for j in {10..12}
do
for k in {10..31}
do
echo "update.sh" 2020 $j $k
./update.sh 2020 $j $k
done
done
for j in {1..9}
do
for k in {1..9}
do
echo "update.sh" 2021 0$j 0$k
./update.sh 2021 0$j 0$k
done
done
for j in {1..9}
do
for k in {10..31}
do
echo "update.sh" 2021 0$j $k
./update.sh 2021 0$j $k
done
done

您可以使用date将输入日期转换为秒,以便进行比较。也用date加一天。

#!/bin/bash
start_date=$(date -I -d "$1")   # Input in format yyyy-mm-dd
end_date=$(date -I)             # Today in format yyyy-mm-dd
echo "Start: $start_date"
echo "Today: $end_date"
d=$start_date                    # In case you want start_date for later?
end_d=$(date -d "$end_date" +%s) # End date in seconds
while [ $(date -d "$d" +%s) -le $end_d ]; do # Check dates in seconds
# Replace `echo` in the below with your command/script
echo ${d//-/ }               # Output the date but replace - with [space]
d=$(date -I -d "$d + 1 day") # Next day
done

在本例中,我使用echo,但将其替换为update.sh的路径。样例输出:

[user@server:~]$ ./dateloop.sh 2021-08-29
Start: 2021-08-29
End  : 2021-09-20
2021 08 29
2021 08 30
2021 08 31
2021 09 01
2021 09 02
2021 09 03
2021 09 04
2021 09 05
2021 09 06
2021 09 07
2021 09 08
2021 09 09
2021 09 10
2021 09 11
2021 09 12
2021 09 13
2021 09 14
2021 09 15
2021 09 16
2021 09 17
2021 09 18
2021 09 19
2021 09 20

最新更新