Number of Days in a Month
If you have to loop through everyday in a month to generate daily summary, you may want to know how many days in a particular month. It may not be as straightforward as you think because you need to take into account of leap year, short months, and so on.
Here is a simple trick that can help you on this. Run the
cal
command to display the calendar and pipe it to awk to pick up the last number from the last non-empty line.
$cal October 2009 Su Mo Tu We Th Fr 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 31 $cal | awk 'NF>0{n=$NF}END{print n}' 31 $cal 2 2012 February 2012 Su Mo Tu We Th Fr 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 $cal 2 2012 | awk 'NF>0{n=$NF}END{print n}' 29
Labels: shell script
0 Comments:
Post a Comment
<< Home