Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement a Shell script to traverse a date range

Shulou Source: shulou.com Published: 2022-06-03 09:22:01 09月12日 Update

This article mainly explains "how to implement Shell script traversal of a date range", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's take you to learn "how to implement Shell script traversal of a date range"!

The code is as follows:

#!/ usr/bin/env bash

date1="$1"

date2="$2"

echo "date1: $date1"

echo "date2: $date2"

tempdate=`date -d "-0 day $date1" +%F`

enddate=`date -d "-0 day $date2" +%F`

tempdateSec=`date -d "-0 day $date1" +%s`

enddateSec=`date -d "-0 day $date2" +%s`

echo "####################################"

echo 'tempdate: '$tempdate

echo 'enddate: '$enddate

#for i in `seq 1 130`; do

for i in `seq 1 300`; do

if [[ $tempdateSec -lt $enddateSec ]]; then

break

fi

echo $tempdate

python pc_jibzhuanti_url.py $tempdate

tempdate=`date -d "-$i day $date1" +%F`

tempdateSec=`date -d "-$i day $date1" +%s`

done

This is a script that outputs time in reverse order. You need to input two time parameters, one is the start time and the other is the end time. For example, input:

The code is as follows:

./ pc_jibzhuanti_url_run.sh 2014-06-30 2014-06-01

The output is:

The code is as follows:

date1: 2014-06-30

date2: 2014-06-01

####################################

tempdate: 2014-06-30

enddate: 2014-06-01

2014-06-30

2014-06-29

2014-06-28

2014-06-27

2014-06-26

2014-06-25

2014-06-24

2014-06-23

2014-06-22

2014-06-21

2014-06-20

2014-06-19

2014-06-18

2014-06-17

2014-06-16

2014-06-15

2014-06-14

2014-06-13

2014-06-12

2014-06-11

2014-06-10

2014-06-09

2014-06-08

2014-06-07

2014-06-06

2014-06-05

2014-06-04

2014-06-03

2014-06-02

2014-06-01

At this point, I believe that everyone has a deeper understanding of "how to implement Shell script traversal of a date range," may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Tags: Scripts time date range code content learning input output utility deeper two reverse order interest parameters practicality practicality simplicity of operation methods more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB MySQL Microsoft OPPO Reno Linux