Get first day of last month and last day of last month in bash
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I post them here cause it shows how powerful date is:
FirstDay
date -d "-1 month -$(($(date +%d)-1)) days"
Lastday
date -d "-$(date +%d) days -1 month"
Greetings!
Just tried these above. Mentioned that the Lastday did not worked well. You don't need the -1 month, becouse it just subtracts the days in this month thus gives the last month.
Actually neither firstday or lastday worked from command line for me. I hadn't tried them before.
They're also both based on the way bash and gnu date operate. The script I posted above them would work on other UNIX flavors as well as Linux though you'd need to change the interpreter to another shell (e.g. ksh) if no on Linux or a system with bash ported to it.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
Quote:
Originally Posted by jlightner
Why hard code the values like that?
As a matter of example. The question was how to find the first and last day of the previous month of a given date.
Obviously you can substitute the hard coded date by a $variable or by "date +%Y-%m-%d" if you like.
Edit: reading your post #7 again, I see that you said "date" didn't work for you on Unix. I missed that, I thought "date" as posted by rudydark didn't work for you, and I thought you added that it would work on GNU only as a sort of general complaint. Oh well, I was wrong,it was late at night yesterday when I wrote my post.
BTW, just because I am interested, why are you (also) running HP-UX, SCO etc? For work, or other reasons?
Actually the "date" command exists on HP-UX and all flavors of UNIX I've come across.
What I was saying was the "lastday" and "firstday" posted by someone after my initial post didn't work for me on Linux. I never even tested them before the other poster said they didn't work for him on Linux yesterday.
As to why the other things:
I'm a professional UNIX Administrator and have been since around 1991. The other systems are used in business. I've worked on many UNIX variants such as SunOS (pre-solaris), Solaris, AIX, Xenix, Astrix, Dynix, A/UX (Apple's erstwhile UNIX that ran alongside System 7) and even the original AT&T branded UNIX in addition to the ones we have here. You'd be surprised at the difference in some of the tools between the UNIX variants. The GNU tools of the same name often have far more functionality than the UNIX tools. On Dynix I had to write myself a fake finger program (or "last" - it's been a while) because it didn't exist and I needed it for a system gather script I was writing for all platforms.
Now a bit more "difficult": I'm using aforementioned one-liners in a script.
I'm running this script every evening at 20:30 and I collect data from the day before. This means:
today (23/11/2009) at 20:30 I'll collect some data and save them in a txt file as:
22/11/2009,dataA,dataB
After having done this, I use gnuplot to automatically generate my histogram of that collected values. Therefore, I have always the histogram for "yesterday".
Now, as I need to define the x-range everytime I run this script, and I want it to be FIRST DAY OF MONTH --> LAST DAY OF MONTH, I'm a bit stuck on how to handle the first day of a month.
This is because of following.
Let's assume it's the first of december (01.12.2009). I'm collecting data from 30.11.2009 and would like to plot it within the boundaries of NOVEMBER. Thus, the one-liner for the "LAST_DAY" is not working anymore, because it would return me "31.12" instead of my desired "30.11" (and also the first day would be wrong, with the above command, but that can be solved easily).
How would you suggest me to do it, considering that in gnuplot there is no "if" possibility?
FirstDay
date -d "-1 month -$(($(date +%d)-1)) days"
Won't work on the 9th day of the month as $(( tries to subtract 09-1. Any number with 0 in front of it is interpreted as an octal number and there is no octal 9.
You will get the following error
09: value too great for base (error token is "09")
Solutions:
# +%-d will remove the zero padding
date -d "-1 month -$(($(date +%-d)-1)) day"
# separate the arithmetic
date -d "-1 month -$(date +%d) day +1 day"
# change 09 into 9 decimal with 10#
date -d "-1 month -$((10#$(date +%d) - 1)) day"
Lastday
date -d "-$(date +%d) day"
Last edited by jach; 08-09-2012 at 05:53 PM.
Reason: Error in script
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.