Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I the get number of days in a month? 6

Status
Not open for further replies.

Givar

Programmer
Nov 7, 2001
5
SE
Hi.

I want to build a sub in a shell-script that can give me the last day of the month (or the number of days in a month), i.e. if I pass in "$year=2001" and "$month=11", the sub should return the number of days in the month November year 2001 (which should be 30). The sub should be able to handle leap-years.

Hope you guys can help out, thanks in advance.
 
# cal $month $year | tail -2 | head -1 | awk '{ print $NF }'

crowe
 
Damn,

Someone got there first :-( A minor refinement:
Code:
cal $month $year | xargs echo | awk '{print $NF}'
The xargs call flattens the calendar. One for the FAQ I think :)
Cheers, Neil
 
what a great tool! (xargs). Never knew that. What does the echo do? I tried "cal | xargs | awk '{ print $NF }'" and it seemed to do the same thing.

crowe
 
You are right crowe. The manual page for xargs says:
Code:
NAME
     xargs - construct argument lists and invoke utility

SYNOPSIS
     xargs  [ -t ]  [ -p ]  [ -e  [ eofstr ]  ]  [ -E eofstr ]  [
     -I replstr  ]   [ -i  [ replstr ]  ]  [ -L number ]  [ -l  [
     number ]  ]  [ -n number  [ -x ]  ]  [ -s size ]  [  utility
     [ argument. ..  ]  ]
What this means is that the standard input to xargs is used to construct a call to another functions. For example, if you wished to compress all *.tar files on your system you could use something like:
Code:
find / -type f -name "*.tar" | xargs compress -f
In this example, compress is the utility. The manual also states:

Code:
     utility   The name of the utility to be  invoked,  found  by
               search  path  using the PATH environment variable;
               see environ(5). If utility is omitted, the default
               is  the  echo(1)  utility.  If the utility operand
               names any of the  special  built-in  utilities  in
               shell_builtins(1), the results are undefined.
So echo is used as the default command.

Cheers, Neil
 
Thanks a lot guys! That's a big help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top