last day of the month in bash

last day of the month in bash

am 30.11.2007 11:16:21 von patrick

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

Re: last day of the month in bash

am 30.11.2007 11:24:37 von patrick

On 30 Nov, 11:16, 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

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

Re: last day of the month in bash

am 30.11.2007 11:57:32 von Janis Papanagnou

On 30 Nov., 11:16, 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

One possibility...

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


Janis

Re: last day of the month in bash

am 30.11.2007 12:03:06 von cichomitiko

"Patrick" wrote in message
news:3c7ce19a-b1c1-48cb-8e2f-0b7bfcfea80e@e25g2000prg.google groups.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

Re: last day of the month in bash

am 30.11.2007 12:03:12 von Janis Papanagnou

On 30 Nov., 11:24, Patrick wrote:
> On 30 Nov, 11:16, 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
>
> 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

Re: last day of the month in bash

am 30.11.2007 12:41:08 von mallin.shetland

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?

Re: last day of the month in bash

am 30.11.2007 12:44:52 von cichomitiko

"mallin.shetland" wrote in message
news:1379725.01WyeyZ0fA@localhost...
> 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?

.... 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

Re: last day of the month in bash

am 30.11.2007 14:01:26 von mallin.shetland

Radoulov, Dimitre scrisse:

>
> "mallin.shetland" wrote in message
> news:1379725.01WyeyZ0fA@localhost...
>> 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?
>
> ... 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.

Re: last day of the month in bash

am 30.11.2007 14:20:30 von patrick

On 30 Nov, 14:01, "mallin.shetland" wrote:
> Radoulov, Dimitre scrisse:
>
>
>
> > "mallin.shetland" wrote in message
> >news:1379725.01WyeyZ0fA@localhost...
> >> 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?
>
> > ... 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.

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!

Re: last day of the month in bash

am 30.11.2007 22:23:13 von scott_layne_gillespie

On Nov 30, 4:16 am, 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

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

Re: last day of the month in bash

am 01.12.2007 00:11:20 von mallin.shetland

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 $_

Re: last day of the month in bash

am 01.12.2007 02:38:29 von Janis Papanagnou

mallin.shetland wrote:
> 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 $_

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

Re: last day of the month in bash

am 01.12.2007 03:28:18 von krahnj

Janis Papanagnou wrote:
>
> mallin.shetland wrote:
> > 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 $_
>
> 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

Re: last day of the month in bash

am 01.12.2007 06:42:26 von Bill Marcum

On 2007-12-01, Janis Papanagnou 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?

Re: last day of the month in bash

am 01.12.2007 09:22:40 von mallin.shetland

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.

Re: last day of the month in bash

am 01.12.2007 10:58:07 von franzi

On 1 Dic, 09:22, "mallin.shetland" wrote:
> 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.

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

Re: last day of the month in bash

am 01.12.2007 12:00:36 von Bill Marcum

On 2007-12-01, franzi 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}'

Re: last day of the month in bash

am 01.12.2007 12:09:53 von Janis Papanagnou

Bill Marcum wrote:
> On 2007-12-01, Janis Papanagnou 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?

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

Re: last day of the month in bash

am 02.12.2007 03:30:07 von cfajohnson

On 2007-11-30, mallin.shetland wrote:
> 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 $_

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
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

Re: last day of the month in bash

am 02.12.2007 12:29:42 von William James

On Nov 30, 4:16 am, 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

Find last day of month, using Ruby:

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

Re: last day of the month in bash

am 03.12.2007 11:24:44 von gazelle

In article <423a32ae-f73f-4f98-a358-9e7e78662d0f@y43g2000hsy.googlegroups.com>,
William James wrote:
>On Nov 30, 4:16 am, 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
>
>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)}'

Re: last day of the month in bash

am 04.12.2007 21:45:23 von cdl

In article ,
Kenny McCormack wrote:
>In article <423a32ae-f73f-4f98-a358-9e7e78662d0f@y43g2000hsy.googlegroups.com>,
>William James wrote:
>>On Nov 30, 4:16 am, 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
>>
>>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)}'
>

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

Re: last day of the month in bash

am 05.12.2007 12:44:34 von patrick

On 4 Dic, 21:45, c...@deeptow.ucsd.edu (Carl Lowenstein) wrote:
> In article ,
>
>
>
> Kenny McCormack wrote:
> >In article <423a32ae-f73f-4f98-a358-9e7e78662...@y43g2000hsy.googlegroups.com>,
> >William James wrote:
> >>On Nov 30, 4:16 am, 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
>
> >>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)}'
>
> 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

Re: last day of the month in bash

am 05.12.2007 14:06:12 von gazelle

In article ,
Carl Lowenstein wrote:
>In article ,
>Kenny McCormack wrote:
>>In article <423a32ae-f73f-4f98-a358-9e7e78662d0f@y43g2000hsy.googlegroups.com>,
>>William James wrote:
>>>On Nov 30, 4:16 am, 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
>>>
>>>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)}'
>>
>
>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) ?

Re: last day of the month in bash

am 05.12.2007 22:01:17 von G_r_a_n_t_

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

>In article ,
>Carl Lowenstein wrote:
>>In article ,
>>Kenny McCormack wrote:
>>>In article <423a32ae-f73f-4f98-a358-9e7e78662d0f@y43g2000hsy.googlegroups.com>,
>>>William James wrote:
>>>>On Nov 30, 4:16 am, 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
>>>>
>>>>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)}'
>>>
>>
>>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) ?

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.

Re: last day of the month in bash

am 20.12.2007 14:45:44 von Snaggles

On 30 Nov., 14:20, Patrick wrote:
> On 30 Nov, 14:01, "mallin.shetland" wrote:
>
> > Radoulov, Dimitre scrisse:
>
> > > "mallin.shetland" wrote in message
> > >news:1379725.01WyeyZ0fA@localhost...
> > >> 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?
>
> > > ... 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.
>
> 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

UUOGaT (Was: last day of the month in bash)

am 20.12.2007 15:05:46 von gazelle

In article ,
Snaggles wrote:
....
>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

UUOGaT

cal $1 $2 | gawk '/^[2-3][0-9]/ {x=$0} END {print T[split(x,T)]}'

Although, as I look at it, I'm not sure why you need the regexp match at
all. I think you can just change the gawk part to:

END {print T[split($0,T)]}

Re: last day of the month in bash

am 20.12.2007 15:13:13 von Ed Morton

On 12/20/2007 7:45 AM, Snaggles wrote:
> On 30 Nov., 14:20, Patrick wrote:
>
>>On 30 Nov, 14:01, "mallin.shetland" wrote:
>>
>>
>>>Radoulov, Dimitre scrisse:
>>
>>>>"mallin.shetland" wrote in message
>>>>news:1379725.01WyeyZ0fA@localhost...
>>>>
>>>>>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?
>>>>
>>>>... 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.
>>
>>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

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,

Re: UUOGaT (Was: last day of the month in bash)

am 20.12.2007 15:20:46 von Ed Morton

On 12/20/2007 8:05 AM, Kenny McCormack wrote:
> In article ,
> Snaggles wrote:
> ...
>
>>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
>
>
> UUOGaT
>
> cal $1 $2 | gawk '/^[2-3][0-9]/ {x=$0} END {print T[split(x,T)]}'
>
> Although, as I look at it, I'm not sure why you need the regexp match at
> all. I think you can just change the gawk part to:
>
> END {print T[split($0,T)]}
>

cal ends with a blank line, otherwise the script could just be "END{print $NF}".

Ed.

Re: UUOGaT (Was: last day of the month in bash)

am 20.12.2007 17:36:36 von gazelle

In article <476A7A3E.6040906@lsupcaemnt.com>,
Ed Morton wrote:
>
>
>On 12/20/2007 8:05 AM, Kenny McCormack wrote:
>> In article ,
>> Snaggles wrote:
>> ...
>>
>>>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
>>
>>
>> UUOGaT
>>
>> cal $1 $2 | gawk '/^[2-3][0-9]/ {x=$0} END {print T[split(x,T)]}'
>>
>> Although, as I look at it, I'm not sure why you need the regexp match at
>> all. I think you can just change the gawk part to:
>>
>> END {print T[split($0,T)]}
>>
>
>cal ends with a blank line, otherwise the script could just be "END{print $NF}".
>
> Ed.
>

Not on my system (Linux), it doesn't:

% cal | tail -1
30 31
% cal 12 2007 | tail -1
30 31
%

Re: UUOGaT (Was: last day of the month in bash)

am 20.12.2007 17:45:12 von Joachim Schmitz

"Kenny McCormack" schrieb im Newsbeitrag
news:fke5mk$a6k$1@news.xmission.com...
> In article <476A7A3E.6040906@lsupcaemnt.com>,
> Ed Morton wrote:
>>
>>
>>On 12/20/2007 8:05 AM, Kenny McCormack wrote:
>>> In article
>>> ,
>>> Snaggles wrote:
>>> ...
>>>
>>>>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
>>>
>>>
>>> UUOGaT
>>>
>>> cal $1 $2 | gawk '/^[2-3][0-9]/ {x=$0} END {print T[split(x,T)]}'
>>>
>>> Although, as I look at it, I'm not sure why you need the regexp match at
>>> all. I think you can just change the gawk part to:
>>>
>>> END {print T[split($0,T)]}
>>>
>>
>>cal ends with a blank line, otherwise the script could just be "END{print
>>$NF}".
>>
>> Ed.
>>
>
> Not on my system (Linux), it doesn't:
>
> % cal | tail -1
> 30 31
> % cal 12 2007 | tail -1
> 30 31
> %
Not this month, but try 'cal 11 2007|tail -1'

Bye, Jojo

Re: UUOGaT (Was: last day of the month in bash)

am 20.12.2007 17:47:17 von Ed Morton

On 12/20/2007 10:36 AM, Kenny McCormack wrote:
> In article <476A7A3E.6040906@lsupcaemnt.com>,
> Ed Morton wrote:
>
>>
>>On 12/20/2007 8:05 AM, Kenny McCormack wrote:
>>
>>>In article ,
>>>Snaggles wrote:
>>>...
>>>
>>>
>>>>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
>>>
>>>
>>>UUOGaT
>>>
>>>cal $1 $2 | gawk '/^[2-3][0-9]/ {x=$0} END {print T[split(x,T)]}'
>>>
>>>Although, as I look at it, I'm not sure why you need the regexp match at
>>>all. I think you can just change the gawk part to:
>>>
>>> END {print T[split($0,T)]}
>>>
>>
>>cal ends with a blank line, otherwise the script could just be "END{print $NF}".
>>
>> Ed.
>>
>
>
> Not on my system (Linux), it doesn't:
>
> % cal | tail -1
> 30 31
> % cal 12 2007 | tail -1
> 30 31
> %
>

I should've been clearer - cal produces 8 lines of output regardless of how many
days are in the month or what weekdays are what day numbers. So, if you pick a
month+year that has just a few days and/or those days are in a specific order,
then you get padding with blank lines at the end. For example:

-------------
$ cal 11 2007 | cat -n
1 November 2007
2 S M Tu W Th F S
3 1 2 3
4 4 5 6 7 8 9 10
5 11 12 13 14 15 16 17
6 18 19 20 21 22 23 24
7 25 26 27 28 29 30
8
$ cal 12 2007 | cat -n
1 December 2007
2 S M Tu W Th F S
3 1
4 2 3 4 5 6 7 8
5 9 10 11 12 13 14 15
6 16 17 18 19 20 21 22
7 23 24 25 26 27 28 29
8 30 31
$ cal 9 1752 | cat -n
1 September 1752
2 S M Tu W Th F S
3 1 2 14 15 16
4 17 18 19 20 21 22 23
5 24 25 26 27 28 29 30
6
7
8
$
-------------

What's up with Sept. 1752 left as an exercise ;-).

Ed.