LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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



Reply
 
Search this Thread
Old 02-01-2007, 04:34 PM   #1
xowl
Member
 
Registered: Jan 2005
Distribution: Slackware current (and others)
Posts: 188

Rep: Reputation: 30
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
 
Old 02-01-2007, 05:11 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
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 05:19 PM.
 
1 members found this post helpful.
Old 02-01-2007, 05:13 PM   #3
xowl
Member
 
Registered: Jan 2005
Distribution: Slackware current (and others)
Posts: 188

Original Poster
Rep: Reputation: 30
Hi,


Thanks a lot. It really helped me.
 
Old 02-01-2007, 05:20 PM   #4
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
No problem.

Make sure you use the edited version I just made. I added a couple of things to it.
 
Old 02-05-2007, 11:28 AM   #5
xowl
Member
 
Registered: Jan 2005
Distribution: Slackware current (and others)
Posts: 188

Original Poster
Rep: Reputation: 30
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"
 
Old 09-08-2009, 05:13 AM   #6
rudydark
LQ Newbie
 
Registered: Nov 2006
Posts: 6

Rep: Reputation: 1
Quote:
Originally Posted by xowl View Post
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.
 
Old 09-08-2009, 08:48 AM   #7
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
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.
 
Old 09-08-2009, 11:47 PM   #8
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,236

Rep: Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545
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
 
Old 09-09-2009, 06:59 AM   #9
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
Why hard code the values like that?
 
Old 09-09-2009, 08:46 AM   #10
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,236

Rep: Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545
Quote:
Originally Posted by jlightner View Post
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 08:55 AM.
 
Old 09-09-2009, 01:22 PM   #11
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
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.
 
Old 11-23-2009, 05:54 AM   #12
bonnyfused
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Rep: Reputation: 0
Quote:
Originally Posted by xowl View Post
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!
 
Old 11-23-2009, 09:44 AM   #13
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
Yes it works for me as well.
 
Old 11-23-2009, 11:44 AM   #14
bonnyfused
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Rep: Reputation: 0
Quote:
Originally Posted by jlightner View Post
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...
 
Old 08-09-2012, 06:52 AM   #15
jach
LQ Newbie
 
Registered: Aug 2012
Posts: 1

Rep: Reputation: Disabled
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 06:53 PM. Reason: Error in script
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
transform month number to month name in php ALInux Programming 1 11-09-2005 11:45 AM
Multicount in PHP , displays visits on current day, week, month, year and total visit xbaez Programming 1 04-24-2005 03:50 AM
bash month variable johniccp Linux - Newbie 1 12-06-2004 02:41 PM
cron last day of every month lobo78 Linux - General 2 03-03-2004 09:41 PM
Starting day of month, month length chrisk5527 Programming 2 03-03-2004 05:03 PM


All times are GMT -5. The time now is 05:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration