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

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > UUOGaT (Was: 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 UUOGaT (Was: last day of the month in bash)
Kenny McCormack

2007-12-20, 1:26 pm

In article <e1931514-804d-4c0d-aeea-7cfdbe6da409@l32g2000hse.googlegroups.com>,
Snaggles <snaggles@gmx.de> 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)]}

Ed Morton

2007-12-20, 1:26 pm



On 12/20/2007 8:05 AM, Kenny McCormack wrote:
> In article <e1931514-804d-4c0d-aeea-7cfdbe6da409@l32g2000hse.googlegroups.com>,
> Snaggles <snaggles@gmx.de> wrote:
> ...
>
>
>
> 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.

Kenny McCormack

2007-12-20, 1:26 pm

In article <476A7A3E.6040906@lsupcaemnt.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
>
>
>On 12/20/2007 8:05 AM, Kenny McCormack wrote:
>
>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
%

Joachim Schmitz

2007-12-20, 1:26 pm

"Kenny McCormack" <gazelle@xmission.xmission.com> schrieb im Newsbeitrag
news:fke5mk$a6k$1@news.xmission.com...
> In article <476A7A3E.6040906@lsupcaemnt.com>,
> Ed Morton <morton@lsupcaemnt.com> wrote:
>
> 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


Ed Morton

2007-12-20, 1:26 pm



On 12/20/2007 10:36 AM, Kenny McCormack wrote:
> In article <476A7A3E.6040906@lsupcaemnt.com>,
> Ed Morton <morton@lsupcaemnt.com> wrote:
>
>
>
> 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.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com