Solved

Unix Script: Loop over all days of a month

Posted on 2016-10-25
17
141 Views
Last Modified: 2016-10-27
Dear Experts,

What I want to do in a bash script, I provide the pseudo code

currentDate=script execution date

for all days in previous month (relative to currentDate)
     currentLoopDate=Date in the format: YYYY-DD-MM
end for

e.g. if I run the script now, October 25

in the loop my first date string should be 2016-01-10
in the loop my second date string should be 2016-02-10
in the loop my last date string should be 2016-31-10
0
Comment
Question by:mruff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 4
  • +1
17 Comments
 
LVL 16

Assisted Solution

by:Hanno P.S.
Hanno P.S. earned 75 total points
ID: 41858882
which date do we have today?
date '+%Y-%m-%d'

Open in new window

The last month would be
expr `date '+%Y%m' - 1` | sed -e 's/\(....\)/\1-'

Open in new window

Now you can start with you counter variable:
day=`expr `date '+%Y%m' - 1` | sed -e 's/\(....\)/\1-'`-01

Open in new window

0
 
LVL 16

Expert Comment

by:Hanno P.S.
ID: 41858883
This variable wil vae to get incremented from day to day
0
 
LVL 5

Accepted Solution

by:
Abhimanyu Suri earned 225 total points
ID: 41858968
Here is an example, please format the output as per your requirement and update ref_days with all months

/home>cat ref_days.txt ### month|days
10|31


/home>cat loop_date.sh
#!/usr/bin/sh

var_currdate=`date "+%Y-%d-%m"`
var_currmonth=`echo ${var_currdate}|awk -F'-' '{print $3}'`
var_date=`echo ${var_currdate}|awk -F'-' '{print $2}'`

echo $var_currdate $var_currmonth ${var_date}

if [[ `grep $var_currmonth ref_days.txt|wc -l` -eq 1 ]]
then
var_loopdays=`cat ref_days.txt|awk -F'|' '{print $2}'`
var_loop1=`expr $((${var_date}-1))`

counter=1

while [[ $counter -le $var_loop1 ]]
do
var_monthstartdate_calc=`expr $((${var_date}-$counter))`
echo "day $counter `date -d "-${var_monthstartdate_calc}days"`"
counter=`expr $(($counter+1))`
done

counter=${var_date}

while [[ ${counter} -le $var_loopdays ]]
do
var_monthstartdate_calc=`expr $(($counter-${var_date}))`
echo "day ${counter} `date -d "+${var_monthstartdate_calc}days"`"
counter=`expr $(($counter+1))`
done

else
echo "invalid month"
fi

/home>. ./loop_date.sh
2016-25-10 10 25
day 1 Sat Oct  1 12:45:56 EDT 2016
day 2 Sun Oct  2 12:45:56 EDT 2016
day 3 Mon Oct  3 12:45:56 EDT 2016
day 4 Tue Oct  4 12:45:56 EDT 2016
day 5 Wed Oct  5 12:45:56 EDT 2016
day 6 Thu Oct  6 12:45:56 EDT 2016
day 7 Fri Oct  7 12:45:56 EDT 2016
day 8 Sat Oct  8 12:45:56 EDT 2016
day 9 Sun Oct  9 12:45:56 EDT 2016
day 10 Mon Oct 10 12:45:56 EDT 2016
day 11 Tue Oct 11 12:45:56 EDT 2016
day 12 Wed Oct 12 12:45:56 EDT 2016
day 13 Thu Oct 13 12:45:56 EDT 2016
day 14 Fri Oct 14 12:45:56 EDT 2016
day 15 Sat Oct 15 12:45:56 EDT 2016
day 16 Sun Oct 16 12:45:56 EDT 2016
day 17 Mon Oct 17 12:45:56 EDT 2016
day 18 Tue Oct 18 12:45:56 EDT 2016
day 19 Wed Oct 19 12:45:56 EDT 2016
day 20 Thu Oct 20 12:45:56 EDT 2016
day 21 Fri Oct 21 12:45:56 EDT 2016
day 22 Sat Oct 22 12:45:56 EDT 2016
day 23 Sun Oct 23 12:45:56 EDT 2016
day 24 Mon Oct 24 12:45:56 EDT 2016
day 25 Tue Oct 25 12:45:56 EDT 2016
day 26 Wed Oct 26 12:45:56 EDT 2016
day 27 Thu Oct 27 12:45:56 EDT 2016
day 28 Fri Oct 28 12:45:56 EDT 2016
day 29 Sat Oct 29 12:45:56 EDT 2016
day 30 Sun Oct 30 12:45:56 EDT 2016
day 31 Mon Oct 31 12:45:56 EDT 2016
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mruff
ID: 41859811
Hi Abhimanu,
thx I am running this script under AIX and getting errors: date: illegal option --d usage: date [-] [+Field Descriptors]
0
 

Author Comment

by:mruff
ID: 41859815
Dear Hanno
I am running the script at AIX
the command
day=`expr `date '+%Y%m' - 1` | sed -e 's/\(....\)/\1-'`-01
gives an error
syntax error at line 1 : '|' unexpecgted
0
 
LVL 16

Expert Comment

by:Hanno P.S.
ID: 41859837
Let's do it step by step:
1. Get today's date as "a number" consisting of current year and month
date '+%Y%m' 

Open in new window

Now try to have the result stored in a variable (and display the variable's content)
day=`date '+%Y%m' `
echo $day

Open in new window

Now, decrement this by one (to get one month back):
day=`expr $day - 1'
echo $day

Open in new window

0
 
LVL 16

Expert Comment

by:Hanno P.S.
ID: 41859840
The srcipt Abhimanu provided looks pretty good to me!
Have you checked if this works?
date "+%Y-%d-%m"

Open in new window

Maybe, you want to try using single instead of double quotes:
var_currdate=`date '+%Y-%d-%m'`
echo $var_currdate

Open in new window

0
 

Author Comment

by:mruff
ID: 41859872
@Hanno
Yes I've checked the script
got an error (have to man date)
errors: date: illegal option --d usage: date [-] [+Field Descriptors]
And I did not fully understand yet, how a correct number of days for a month is picked if the ref_days.txt contains all the monts|days
0
 
LVL 16

Expert Comment

by:Hanno P.S.
ID: 41859888
Does this work?
date '+%Y-%d-%m'

Open in new window


If not, check you man page for date's format string options
0
 
LVL 12

Expert Comment

by:tel2
ID: 41859909
Hi mruff,

Good to say up front what flavour of UNIX you're using, especially if it's something like AIX.

I think AIX probably doesn't ship with GNU date.

But to confirm, what output does this give you?:
date -d 'next month'

Open in new window

And this?:
date -v -1m +%Y%m%d

Open in new window


And will you be running this as root, or as another user?  (It could make a difference to my approach.  I can explain later if required.)
0
 
LVL 12

Assisted Solution

by:tel2
tel2 earned 200 total points
ID: 41860964
Hi again nruff,

I decided on a different strategy.

Firstly, where you wrote this:
    "for all days in previous month (relative to currentDate)"
I assume you mean this:
    "for all days in current month (relative to currentDate)"
based on your example.  Right?

#!/bin/bash

currentDate=`date +'%Y-%d-%m'`
echo "currentDate = $currentDate"  # Remove this if you don't need it
yyyy=`echo $currentDate | cut -c-4`
mm=`echo $currentDate | cut -c9-`
daysInMonth=`cal | tr '\n' ' ' | awk '{print $NF}'`
day=1
while [ $day -le $daysInMonth ]
do
        currentLoopDate=`printf %d-%02d-%02d $yyyy $day $mm`
        echo $currentLoopDate    # Remove this if you don't need it
        let day=$day+1
done

Open in new window

Here's the output:
currentDate = 2016-27-10
2016-01-10
2016-02-10
2016-03-10
2016-04-10
2016-05-10
2016-06-10
2016-07-10
2016-08-10
2016-09-10
2016-10-10
2016-11-10
2016-12-10
2016-13-10
2016-14-10
2016-15-10
2016-16-10
2016-17-10
2016-18-10
2016-19-10
2016-20-10
2016-21-10
2016-22-10
2016-23-10
2016-24-10
2016-25-10
2016-26-10
2016-27-10
2016-28-10
2016-29-10
2016-30-10
2016-31-10

Is that what you want?
0
 

Author Comment

by:mruff
ID: 41862121
thank you both!
0
 

Author Comment

by:mruff
ID: 41862135
@tel2 sorry did not notice your answer until now
I've requested from moderator to select yours as best solution and provide 300 point to you
0
 
LVL 12

Expert Comment

by:tel2
ID: 41863234
Thanks for that, mruff.

tel2
0
 

Author Comment

by:mruff
ID: 41863372
@all, I reassigned the points as Abhimuanyu posted first and most likely put most effort in his solution I've assigned him a little more point then to you tel2.
I hope the points assignment is considered fair by all of you
thank you all for your assistance, with all your help I've finished my script
0
 
LVL 12

Expert Comment

by:tel2
ID: 41863377
Which solution are you going to use, mruff?
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question