How to use awk to get the amount of days of a specific month1 min read

In

I was busy writing a shell script on my Linux machine to generate screenshots for liveuamap.com. I needed a way to get the amount of days in a specific month for a specific date. After a long search, I found ways to do it, one way was to use cal:

(cal 5 2015 | awk 'NF {DAYS = $NF}; END {print DAYS}')

The command will return 31 which is the correct amount of days for May in 2015.
if you want to get the number in a variable, and that is actually what I needed. Just edit the code like this:

daysAmount = $(cal 5 2015 | awk 'NF {DAYS = $NF}; END {print DAYS}')
echo $daysAmount

Many thanks to Milosz Galazka who has answered my question. Here is a link to his article
https://blog.sleeplessbeastie.eu/2013/03/24/how-to-get-number-of-days-in-a-month-using-shell-commands/

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.