Linux - Software This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-01-2007, 03:34 PM
|
#1
|
|
Member
Registered: Jan 2005
Distribution: Slackware current (and others)
Posts: 188
Rep:
|
Get first day of last month and last day of last month in bash
Hi there
I want to get the first day of last month, and the last day of last month in bash.
Any ideas?
Camilo
Example: If in Feb - 27 I run the command, I should get
FirstDay: 01-01-2007
LastDay: 31-01-2007
but If in Mar 3 I run the command, I should get
FirstDay: 01-02-2007
LastDay: 28-02-2007
Thanx in advance
|
|
|
|
02-01-2007, 04:11 PM
|
#2
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
Code:
#!/bin/bash
set `date +%m" "%Y`
CURMTH=$1
CURYR=$2
if [ $CURMTH -eq 1 ]
then PRVMTH=12
PRVYR=`expr $CURYR - 1`
else PRVMTH=`expr $CURMTH - 1`
PRVYR=$CURYR
fi
if [ $PRVMTH -lt 10 ]
then PRVMTH="0"$PRVMTH
fi
LASTDY=`cal $PRVMTH $PRVYR | egrep "28|29|30|31" |tail -1 |awk '{print $NF}'`
echo First Day: 01-$PRVMTH-$PRVYR
echo Last Day: $LASTDY-$PRVMTH-$PRVYR
Edit: I added the padding for month so now Feb will show as 02 instead of 2.
Last edited by MensaWater; 02-01-2007 at 04:19 PM.
|
|
|
1 members found this post helpful.
|
02-01-2007, 04:13 PM
|
#3
|
|
Member
Registered: Jan 2005
Distribution: Slackware current (and others)
Posts: 188
Original Poster
Rep:
|
Hi,
Thanks a lot. It really helped me.
|
|
|
|
02-01-2007, 04:20 PM
|
#4
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
No problem.
Make sure you use the edited version I just made. I added a couple of things to it.
|
|
|
|
02-05-2007, 10:28 AM
|
#5
|
|
Member
Registered: Jan 2005
Distribution: Slackware current (and others)
Posts: 188
Original Poster
Rep:
|
Hi, I found a faster way to do this.
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"
|
|
|
|
09-08-2009, 04:13 AM
|
#6
|
|
LQ Newbie
Registered: Nov 2006
Posts: 4
Rep:
|
Quote:
Originally Posted by xowl
Hi, I found a faster way to do this.
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.
|
|
|
|
09-08-2009, 07:48 AM
|
#7
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
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.
|
|
|
|
09-08-2009, 10:47 PM
|
#8
|
|
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Etch/Lenny/Squeeze
Posts: 3,792
|
Firstday:
date -d "2007-03-02 -1 month" +%Y-%m-01
2007-02-01
Lastday:
date -d "$(date -d "2007-03-02 -1 month" +%Y-%m-01) +1 month -1 day" +%Y-%m-%d
2007-02-28
jlinkels
|
|
|
|
09-09-2009, 05:59 AM
|
#9
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
Why hard code the values like that?
|
|
|
|
09-09-2009, 07:46 AM
|
#10
|
|
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Etch/Lenny/Squeeze
Posts: 3,792
|
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?
jlinkels
Last edited by jlinkels; 09-09-2009 at 07:55 AM.
|
|
|
|
09-09-2009, 12:22 PM
|
#11
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
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.
|
|
|
|
11-23-2009, 04:54 AM
|
#12
|
|
LQ Newbie
Registered: Nov 2009
Posts: 2
Rep:
|
Quote:
Originally Posted by xowl
Hi, I found a faster way to do this.
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"
|
Are following OK for FIRST DAY CURRENT MONTH and LAST DAY CURRENT MONTH?
I tried them and they seemed OK, just would like to get your confirmation:
First day current month
date -d "-$(($(date +%d)-1)) days"
Last day current month
date -d "+1 month -$(date +%d) days"
Thanks!
|
|
|
|
11-23-2009, 08:44 AM
|
#13
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
Yes it works for me as well.
|
|
|
|
11-23-2009, 10:44 AM
|
#14
|
|
LQ Newbie
Registered: Nov 2009
Posts: 2
Rep:
|
Quote:
Originally Posted by jlightner
Yes it works for me as well.
|
Thanks for your reply.
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?
Thanks...
|
|
|
|
08-09-2012, 05:52 AM
|
#15
|
|
LQ Newbie
Registered: Aug 2012
Posts: 1
Rep: 
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:54 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|