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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This variable wil vae to get incremented from day to day
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Abhimanu,
thx I am running this script under AIX and getting errors: date: illegal option --d usage: date [-] [+Field Descriptors]
thx I am running this script under AIX and getting errors: date: illegal option --d usage: date [-] [+Field Descriptors]
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
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
1. Get today's date as "a number" consisting of current year and month
date '+%Y%m'
Now try to have the result stored in a variable (and display the variable's content)day=`date '+%Y%m' `
echo $day
Now, decrement this by one (to get one month back):day=`expr $day - 1'
echo $day
The srcipt Abhimanu provided looks pretty good to me!
Have you checked if this works?
Have you checked if this works?
date "+%Y-%d-%m"
Maybe, you want to try using single instead of double quotes:var_currdate=`date '+%Y-%d-%m'`
echo $var_currdate
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
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?
If not, check you man page for date's format string options
date '+%Y-%d-%m'
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?:
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.)
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'
And this?:date -v -1m +%Y%m%d
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you both!
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
I've requested from moderator to select yours as best solution and provide 300 point to you
Thanks for that, mruff.
tel2
tel2
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
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?