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.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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 Wheezy/Jessie/Sid, Linux Mint DE
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:
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?