Cogito ergo sum

How to use awk to get the amount of days of a specific month

I was busy with 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 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/

 

About the author

Peshmerge Morad

A IT-student and a programmer based in the Netherlands whose interests span multiple fields.

Add comment

By Peshmerge Morad
Cogito ergo sum

Privacy Preference Center