Cogito ergo sum

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

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}')</pre> The command will return <span class="stackoverflow-codestyle" style="background-color: #eff0f1; font-family: monospace, monospace; font-size: 1em; white-space: pre-wrap; padding: 5px;">31 </span>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: <pre><span class="x_font">daysAmount=(cal 5 2015 | awk 'NF {DAYS = NF}; END {print DAYS}')</span> <span class="x_font">echodaysAmount

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 machine learning & software engineer based in Germany, whose interests span multiple fields.

Add comment

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

Cogito ergo sum