Unix Shell - last day of the month in bash

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > last day of the month in bash





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author last day of the month in bash
Patrick

2007-11-30, 7:35 am

Hi all,
could you tell me if exists a method to know the last day of the month
by command line(bash)?
for example a particular attributo for date or cal
Thx
Patrick

2007-11-30, 7:35 am

On 30 Nov, 11:16, Patrick <patrick.mor...@gmail.com> wrote:
> Hi all,
> could you tell me if exists a method to know the last day of the month
> by command line(bash)?
> for example a particular attributo for date or cal
> Thx


looking the forum i found the solution:
cal | tail -2 | awk '{print $NF}'
Thanks
Janis

2007-11-30, 7:35 am

On 30 Nov., 11:16, Patrick <patrick.mor...@gmail.com> wrote:
> Hi all,
> could you tell me if exists a method to know the last day of the month
> by command line(bash)?
> for example a particular attributo for date or cal
> Thx


One possibility...

cal | awk 'NF{x=$NF}END{print x}'


Janis
Radoulov, Dimitre

2007-11-30, 7:35 am


"Patrick" <patrick.moresi@gmail.com> wrote in message
news:3c7ce19a-b1c1-48cb-8e2f-0b7bfcfea80e@e25g2000prg.googlegroups.com...
> Hi all,
> could you tell me if exists a method to know the last day of the month
> by command line(bash)?
> for example a particular attributo for date or cal
> Thx


Another:

:$(cal)
echo "$_"


Dimitre


Janis

2007-11-30, 7:35 am

On 30 Nov., 11:24, Patrick <patrick.mor...@gmail.com> wrote:
> On 30 Nov, 11:16, Patrick <patrick.mor...@gmail.com> wrote:
>
>
> looking the forum i found the solution:
> cal | tail -2 | awk '{print $NF}'


cal | tail -2 | awk 'NF{print $NF}'

....to suppress the empty line.

> Thanks


mallin.shetland

2007-11-30, 7:35 am

Patrick scrisse:

> ... to know the last day of the month...



date -d "$(date -d month +%Y-%m-1) -1 day" +%d

info date is too hard?
Radoulov, Dimitre

2007-11-30, 7:35 am


"mallin.shetland" <mallin.shetland@aol.com> wrote in message
news:1379725.01WyeyZ0fA@localhost...
> Patrick scrisse:
>
>
>
> date -d "$(date -d month +%Y-%m-1) -1 day" +%d
>
> info date is too hard?


.... if you don't have GNU date?

$ date -d "$(date -d month +%Y-%m-1) -1 day" +%d
date: illegal option -- d
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
date: illegal option -- d
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]


Dimitre


mallin.shetland

2007-11-30, 7:35 am

Radoulov, Dimitre scrisse:

>
> "mallin.shetland" <mallin.shetland@aol.com> wrote in message
> news:1379725.01WyeyZ0fA@localhost...
>
> ... if you don't have GNU date?
> [et cetera]



Yea, it' true.

But perhaps you don't read the original post;
Patrick wrote:

> Hi all,
> could you tell me if exists a method to know the last day of the month
> by command line(bash)?
> for example a particular attributo for date or cal
> Thx


Have you read?

Patrick wrote: "... for example a particular attributo for date or cal"

AFAIK GNU date can do this task so I presume he is speaking of GNU date
and he is quite lazy because he don't read GNU info page's examples.

But I'm wrong because I had to say my example is only for GNU date.
Sorry.


PS For FreeBSD the -d option is for daylight saving system; the -v option
performs similar tasks of GNU data's --date option.







Patrick

2007-11-30, 7:35 am

On 30 Nov, 14:01, "mallin.shetland" <mallin.shetl...@aol.com> wrote:
> Radoulov, Dimitre scrisse:
>
>
>
>
>
>
>
>
> Yea, it' true.
>
> But perhaps you don't read the original post;
>
> Patrick wrote:
>
> Have you read?
>
> Patrick wrote: "... for example a particular attributo for date or cal"
>
> AFAIK GNU date can do this task so I presume he is speaking of GNU date
> and he is quite lazy because he don't read GNU info page's examples.
>
> But I'm wrong because I had to say my example is only for GNU date.
> Sorry.
>
> PS For FreeBSD the -d option is for daylight saving system; the -v option
> performs similar tasks of GNU data's --date option.


indeed i use the command on solaris 10 and "date -d "$(date -d month +
%Y-%m-1) -1 day" +%d " doesn't work.
I used "cal | tail -2 | awk '{print $NF}'" for my script
Thanks to all for support!
scott_layne_gillespie@hotmail.com

2007-11-30, 7:23 pm

On Nov 30, 4:16 am, Patrick <patrick.mor...@gmail.com> wrote:
> Hi all,
> could you tell me if exists a method to know the last day of the month
> by command line(bash)?
> for example a particular attributo for date or cal
> Thx


for i in `cal | tail -2`; do echo $i; done | tail -1
mallin.shetland

2007-11-30, 7:23 pm

Janis scrisse:

> cal | tail -2 | awk 'NF{print $NF}'
>
> ...to suppress the empty line.


I think this thread definitevily closed
but I found this strange bug.

$ date
sab dic 1 00:06:06 CET 2007
$ cal | tail -2 | awk 'NF{print $NF}'
29
31
$

I think the best solution is

:`cal`;echo $_







Janis Papanagnou

2007-12-01, 1:36 am

mallin.shetland wrote:
> Janis scrisse:
>
>
>
>
> I think this thread definitevily closed
> but I found this strange bug.
>
> $ date
> sab dic 1 00:06:06 CET 2007
> $ cal | tail -2 | awk 'NF{print $NF}'
> 29
> 31
> $
>
> I think the best solution is
>
> :`cal`;echo $_


While this works on bash (which the OP uses) you cannot rely on it
with other POSIX shells. In Kornshell the $_ is a reference to the
last argument on the _previous_ line. So it's better to split it in
two lines.

: $(cal)
echo $_

Unfortunately even this doesn't work on some systems. On a WinDOS
machine using MKS and Cygwin resp. this produced an empty word. It
could be fixed, though, by writing it as

: $(cal | tr -d '\n') # it worked as well to delete '\r' instead
echo $_

But the problem is a different one; cal implementations seem to vary
across different platforms. The cal on the WinDOS box I was talking
does produce an empty line at the end, while on my Linux box it does
not. The OP added the tail -2, so we have to assume he also has to
suppress the empty line, while your cal doesn't produce it (as on my
Linux box) and you see the two lines as you've shown above.

Therefore my original solution that I posted

cal | awk 'NF{x=$NF}END{print x}'

which works with both cal output formats and with bash as well as
with ksh (and other shells).

Janis
John W. Krahn

2007-12-01, 1:36 am

Janis Papanagnou wrote:
>
> mallin.shetland wrote:
>
> While this works on bash (which the OP uses) you cannot rely on it
> with other POSIX shells. In Kornshell the $_ is a reference to the
> last argument on the _previous_ line. So it's better to split it in
> two lines.
>
> : $(cal)
> echo $_
>
> Unfortunately even this doesn't work on some systems. On a WinDOS
> machine using MKS and Cygwin resp. this produced an empty word. It
> could be fixed, though, by writing it as
>
> : $(cal | tr -d '\n') # it worked as well to delete '\r' instead
> echo $_
>
> But the problem is a different one; cal implementations seem to vary
> across different platforms. The cal on the WinDOS box I was talking
> does produce an empty line at the end, while on my Linux box it does
> not. The OP added the tail -2, so we have to assume he also has to
> suppress the empty line, while your cal doesn't produce it (as on my
> Linux box) and you see the two lines as you've shown above.
>
> Therefore my original solution that I posted
>
> cal | awk 'NF{x=$NF}END{print x}'
>
> which works with both cal output formats and with bash as well as
> with ksh (and other shells).


This should work as well:

perl -le'print`cal`=~/.+\D(\d+)/s'



John
--
use Perl;
program
fulfillment
Bill Marcum

2007-12-01, 1:36 am

On 2007-12-01, Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote:
>
> But the problem is a different one; cal implementations seem to vary
> across different platforms. The cal on the WinDOS box I was talking
> does produce an empty line at the end, while on my Linux box it does
> not.


Are you sure it isn't that this month the cal output doesn't end with an
empty line, because it prints six rows of numbers?

mallin.shetland

2007-12-01, 7:32 am

Janis Papanagnou scrisse:

> ...
> : $(cal | tr -d '\n') # it worked as well to delete '\r' instead
> echo $_
> ...


No, don't work; on my system:

$ date
mer set 12 00:00:00 CEST 2007
$ :$(cal 9 2007 | tr -d '\n');echo $_
2930
$ : $(cal | tr -d '\n') # it worked as well to delete '\r' instead
echo $_
2930
$

This because odd formatting of cal output:

$ cal 9 2007
settembre 2007
do lu ma me gi ve sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
$ cal 9 2007 | tr -d '\n'
settembre 2007 do lu ma me gi ve sa 1 2 3 4 5 6
7 8 9 10 11 12 13 14 1516 17 18 19 20 21 2223 24 25 26 27 28 2930
$

I prefer:

cal | tr '\n' ' '

Also other bug you have reported should be fixed.

franzi

2007-12-01, 7:32 am

On 1 Dic, 09:22, "mallin.shetland" <mallin.shetl...@aol.com> wrote:
> Janis Papanagnou scrisse:
>
>
> No, don't work; on my system:
>
> $ date
> mer set 12 00:00:00 CEST 2007
> $ :$(cal 9 2007 | tr -d '\n');echo $_
> 2930
> $ : $(cal | tr -d '\n') # it worked as well to delete '\r' instead
> echo $_
> 2930
> $
>
> This because odd formatting of cal output:
>
> $ cal 9 2007
> settembre 2007
> do lu ma me gi ve sa
> 1
> 2 3 4 5 6 7 8
> 9 10 11 12 13 14 15
> 16 17 18 19 20 21 22
> 23 24 25 26 27 28 29
> 30
> $ cal 9 2007 | tr -d '\n'
> settembre 2007 do lu ma me gi ve sa 1 2 3 4 5 6
> 7 8 9 10 11 12 13 14 1516 17 18 19 20 21 2223 24 25 26 27 28 2930
> $
>
> I prefer:
>
> cal | tr '\n' ' '
>
> Also other bug you have reported should be fixed.


I'm sorry about the thread,maybe i din't understand the question but i
tried the examples,that all of you suggested but in macosx there are
some incongruences,like
localhost:~ hazz$ cal
December 2007
S M Tu W Th F S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31


cal | tail -2| awk '{print $NF}'
29
31
as you can see it's wrong in human readable method,30 is missed
Bill Marcum

2007-12-01, 7:32 am

On 2007-12-01, franzi <hazzino@gmail.com> wrote:
> I'm sorry about the thread,maybe i din't understand the question but i
> tried the examples,that all of you suggested but in macosx there are
> some incongruences,like
> localhost:~ hazz$ cal
> December 2007
> S M Tu W Th F S
> 1
> 2 3 4 5 6 7 8
> 9 10 11 12 13 14 15
> 16 17 18 19 20 21 22
> 23 24 25 26 27 28 29
> 30 31
>
>
> cal | tail -2| awk '{print $NF}'
> 29
> 31
> as you can see it's wrong in human readable method,30 is missed

In this thread, we were only interested in the last day of the month.
The "tail -2" is because most versions of cal, in most months, will
output a blank line as the last line. Instead of tail, you could use
only cal and awk:
cal | awk 'NF{last=$NF} END{print last}'




Janis Papanagnou

2007-12-01, 7:32 am

Bill Marcum wrote:
> On 2007-12-01, Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote:
>
>
> Are you sure it isn't that this month the cal output doesn't end with an
> empty line, because it prints six rows of numbers?


Doh! You're right. My bad that I have checked the two different systems
during a month change; I didn't notice and draw a wrong conclusion. :-(
Thanks for pointing that out!

Janis
Chris F.A. Johnson

2007-12-02, 1:36 am

On 2007-11-30, mallin.shetland wrote:
> Janis scrisse:
>
>
> I think this thread definitevily closed
> but I found this strange bug.
>
> $ date
> sab dic 1 00:06:06 CET 2007
> $ cal | tail -2 | awk 'NF{print $NF}'
> 29
> 31
> $
>
> I think the best solution is
>
>:`cal`;echo $_


Apart from the problems mentioned in other posts, I would avoid
using a command that is non standard and whose output format is
unspecified. (I have seen a version of cal which prints three
months by default.)

Getting the days in any month other than February is trivial
without using an external command; February is not much harder
(all you need is to know whether the year is a leap year).
Functions for these calculations have been posted in this group
many times.


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
William James

2007-12-02, 7:32 am

On Nov 30, 4:16 am, Patrick <patrick.mor...@gmail.com> wrote:
> Hi all,
> could you tell me if exists a method to know the last day of the month
> by command line(bash)?
> for example a particular attributo for date or cal
> Thx


Find last day of month, using Ruby:

ruby -r date -e 'puts Date.new(Time.now.year,Time.now.month,-1).day'
Kenny McCormack

2007-12-03, 7:33 am

In article <423a32ae-f73f-4f98-a358-9e7e78662d0f@y43g2000hsy.googlegroups.com>,
William James <w_a_x_man@yahoo.com> wrote:
>On Nov 30, 4:16 am, Patrick <patrick.mor...@gmail.com> wrote:
>
>Find last day of month, using Ruby:
>
>ruby -r date -e 'puts Date.new(Time.now.year,Time.now.month,-1).day'


Or, in gawk:

gawk 'BEGIN {print strftime("%c",mktime(strftime("%Y %m 1 0 0 0"))-86400)}'

Carl Lowenstein

2007-12-04, 7:25 pm

In article <fj0lhc$qh3$2@news.xmission.com>,
Kenny McCormack <gazelle@xmission.xmission.com> wrote:
>In article <423a32ae-f73f-4f98-a358-9e7e78662d0f@y43g2000hsy.googlegroups.com>,
>William James <w_a_x_man@yahoo.com> wrote:
>
>Or, in gawk:
>
>gawk 'BEGIN {print strftime("%c",mktime(strftime("%Y %m 1 0 0 0"))-86400)}'
>


Does that not give you the date one day previous to the first of this
month. Thus the last day of _last_ month.

carl
--
carl lowenstein marine physical lab, u.c. san diego
clowenstein@ucsd.edu
Patrick

2007-12-05, 7:32 am

On 4 Dic, 21:45, c...@deeptow.ucsd.edu (Carl Lowenstein) wrote:
> In article <fj0lhc$qh...@news.xmission.com>,
>
>
>
> Kenny McCormack <gaze...@xmission.xmission.com> wrote:
>
>
>
>
>
> Does that not give you the date one day previous to the first of this
> month. Thus the last day of _last_ month.
>
> carl
> --
> carl lowenstein marine physical lab, u.c. san diego
> clowenst...@ucsd.edu


i haven't gawk but my final solution is:
cal ${MONTH} ${YEARY} | tr '\n' ' ' | awk '{print $NF}'

thanks
Kenny McCormack

2007-12-05, 7:32 am

In article <fj4e93$2vt2$1@ihnp4.ucsd.edu>,
Carl Lowenstein <cdl@deeptow.ucsd.edu> wrote:
>In article <fj0lhc$qh3$2@news.xmission.com>,
>Kenny McCormack <gazelle@xmission.xmission.com> wrote:
>
>Does that not give you the date one day previous to the first of this
>month. Thus the last day of _last_ month.


Yes. For whatever reason, I read that as the requirement.

Adjusting it to fit the other requirement left as an exercise.

P.S. The ruby code above just prints "31" (as this is December). If
that's really all that's required, wouldn't the simplest method just be
a lookup table based on the current month (with a hack for February) ?

Grant

2007-12-05, 7:23 pm

On Wed, 5 Dec 2007 13:06:12 +0000 (UTC), gazelle@xmission.xmission.com (Kenny McCormack) wrote:

>In article <fj4e93$2vt2$1@ihnp4.ucsd.edu>,
>Carl Lowenstein <cdl@deeptow.ucsd.edu> wrote:
>
>Yes. For whatever reason, I read that as the requirement.
>
>Adjusting it to fit the other requirement left as an exercise.
>
>P.S. The ruby code above just prints "31" (as this is December). If
>that's really all that's required, wouldn't the simplest method just be
>a lookup table based on the current month (with a hack for February) ?


Playing with the notion of checking if tomorrow is the first of the month
to decide if today is the last day of the month -- then Feb 29 not an issue:

~$ gawk 'BEGIN{if(strftime("%d",mktime(strftime("%Y %m %d")" 12 0 0")+24*60*60) == 1)print"last day of month";else print"not yet"}'

Grant.
Snaggles

2007-12-20, 1:26 pm

On 30 Nov., 14:20, Patrick <patrick.mor...@gmail.com> wrote:
> On 30 Nov, 14:01, "mallin.shetland" <mallin.shetl...@aol.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> indeed i use the command on solaris 10 and "date -d "$(date -d month +
> %Y-%m-1) -1 day" +%d " doesn't work.
> I used "cal | tail -2 | awk '{print $NF}'" for my script
> Thanks to all for support!


Hello,

i wrote me a script "lastday" which works based on your cal idea:

#!/bin/ksh
# last day in month
if [ $# -ne 2 ]
then
echo "Usage: $0 month year"
else
cal $1 $2 | grep "^[2-3][0-9]" | tail -1 | awk '{print $NF}'
fi

greets
andy
Ed Morton

2007-12-20, 1:26 pm



On 12/20/2007 7:45 AM, Snaggles wrote:
> On 30 Nov., 14:20, Patrick <patrick.mor...@gmail.com> wrote:
>
>
>
> Hello,
>
> i wrote me a script "lastday" which works based on your cal idea:
>
> #!/bin/ksh
> # last day in month
> if [ $# -ne 2 ]
> then
> echo "Usage: $0 month year"
> else
> cal $1 $2 | grep "^[2-3][0-9]" | tail -1 | awk '{print $NF}'
> fi
>
> greets
> andy


No need for tail and grep, this'll do it with any modern awk (e.g. nawk, gawk,
/usr/xpg4/bin/awk on Solaris):

cal $1 $2 | awk 'NF{d=$NF}END{print d}'

and this for old, broken awk (/usr/bin/awk on Solaris):

cal $1 $2 | awk 'NF>0{d=$NF}END{print d}'

Regards,

Ed,

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com