定义脚本 vim calculateDate.sh 如下:
startDate="20210801"
endDate="20210826"
echo "起始日期:$startDate"
echo "结束日期:$endDate"
startDateTime=`date -d "$startDate" +%s`
endDateTime=`date -d "$endDate" +%s`
diff=`expr $endDateTime - $startDateTime`
diffCount=$(expr $diff/86400)
echo "两个日期相差天数(表达式):$diffCount"
echo "两个日期相差天数(整数):$[diffCount]"
echo "输出两个日期之间的所有日期:"
for((i=0;i<=$[diffCount];i++))
do
currentDate=`date -d "$startDate $i day" +%Y%m%d`
echo $currentDate
done
执行脚本
/bin/sh calculateDate.sh
执行结果
起始日期:20210801
结束日期:20210826
两个日期相差天数(表达式):2160000/86400
两个日期相差天数(整数):25
输出两个日期之间的所有日期:
20210801
20210802
20210803
20210804
20210805
20210806
20210807
20210808
20210809
20210810
20210811
20210812
20210813
20210814
20210815
20210816
20210817
20210818
20210819
20210820
20210821
20210822
20210823
20210824
20210825
20210826
|