Re: script to loop through dates

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




Sent from ProtonMail, encrypted email based in Switzerland.

Sent with Proton Mail secure email.

On Friday, June 27th, 2025 at 7:02 AM, Will McDonald <wmcdonald@xxxxxxxxx> wrote:

> On Fri, 27 Jun 2025 at 12:32, olivares33561 via users <users@xxxxxxxxxxxxxxxxxxxxxxx> wrote:
> 
> > I am struggling to create a script that generates a series of dates with special "+%Y.%m.%d" like the following
> > 
> > 2025.06.01
> > 2025.06.01
> > 2025.06.01
> > 
> > I found several examples but can't succeed to get what I want
> > ------
> > 
> > #!/bin/bash
> > start=$1
> > end=$2
> > 
> > start=$(date -d $start +%Y%m%d)
> > end=$(date -d $end +%Y%m%d)
> > 
> > while [[ $start -le $end ]]
> > do
> > echo $start
> > start=$(date -d"$start + 1 day" +"%Y%m%d")
> > done
> > ------
> 
> 
> The date command can't parse dates with periods/dots in this step: start=$(date -d $start +%Y%m%d)
> 
> Example:
> 
> wmcdonald@DESKTOP-9HGJE25:~$ echo $IN
> 2025.07.01
> wmcdonald@DESKTOP-9HGJE25:~$ date -d $IN +%Y%m%d
> date: invalid date ‘2025.07.01’
> wmcdonald@DESKTOP-9HGJE25:~$
> 
> So that step's going to fail. And the while loop can't evaluate the dotted notation strings for greater/less than. The easiest thing to do is strip the dots from the input, then add them back in in the output. This isn't elegant, and someone may provide a better answer but this works:
> 
> #!/bin/bash
> start=$1
> end=$2
> 
> start=$(date -d $(echo $start | sed 's/\./-/g') +%Y%m%d)
> end=$(date -d $(echo $end | sed 's/\./-/g') +%Y%m%d)
> 
> date -d"$start" +%Y.%m.%d
> 
> while [[ $start -lt $end ]]
> do
> date -d"$start + 1 day" +%Y.%m.%d
> start=$(date -d"$start + 1 day" +%Y%m%d)
> done
> 
> wmcdonald@DESKTOP-9HGJE25:~$ ./daterange.sh 2025.07.01 2025.07.16
> 2025.07.01
> 2025.07.02
> 2025.07.03
> 2025.07.04
> 2025.07.05
> 2025.07.06
> 2025.07.07
> 2025.07.08
> 2025.07.09
> 2025.07.10
> 2025.07.11
> 2025.07.12
> 2025.07.13
> 2025.07.14
> 2025.07.15
> 2025.07.16
> 
> 
> 

Yes Sir.  It does the job.  It works but it doesn't print four iterations of the date. 
A for loop inside will do it, but I am not sure how to it?

In here,
> do
> date -d"$start + 1 day" +%Y.%m.%d
> start=$(date -d"$start + 1 day" +%Y%m%d)

A for i=1..4 and print four versions of each date. 

But my skills aren't that great.

Best Regards,


Antonio 

-- 
_______________________________________________
users mailing list -- users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe send an email to users-leave@xxxxxxxxxxxxxxxxxxxxxxx
Fedora Code of Conduct: https://docs.fedoraproject.org/en-US/project/code-of-conduct/
List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines
List Archives: https://lists.fedoraproject.org/archives/list/users@xxxxxxxxxxxxxxxxxxxxxxx
Do not reply to spam, report it: https://pagure.io/fedora-infrastructure/new_issue



[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux