Link to home
Start Free TrialLog in
Avatar of mruff
mruff

asked on

Unix Script: Loop over all days of a month

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
SOLUTION
Avatar of Hanno P.S.
Hanno P.S.
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This variable wil vae to get incremented from day to day
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mruff
mruff

ASKER

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

ASKER

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
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

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

Avatar of mruff

ASKER

@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
Does this work?
date '+%Y-%d-%m'

Open in new window


If not, check you man page for date's format string options
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.)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mruff

ASKER

thank you both!
Avatar of mruff

ASKER

@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
Thanks for that, mruff.

tel2
Avatar of mruff

ASKER

@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
Which solution are you going to use, mruff?