Calculating a calendar in bash

I want to do some calendar manipulations in bash - in particular, I want to find out the last date of a given month (including a leap year, and preparing a table for a search is an invalid solution for me).

Presumably I have the following code:

$year=2009
$start_month=2
$end_month=10
for $month in $(seq $start_month $end_month); do
  echo "Last date of "$(date +"%B" -d "${year}-${month}-01")" is: " ???
done

I can’t figure out how to do this. Although I date -dwould work as POSIX mktime and fold invalid dates with their valid equivalents, so I could say something like date -d "2009-03-00"and get "2009-02-28", but there wasn’t such luck.

Is there a way to do this using only what is available on bash in the standard GNU environment?

+3
source share
4 answers

: date -d 'yesterday 2009-03-01'

. , POSIX.

+6

date (1) -d GNU; GNU Linux.

( sh AFAIK):

: $(cal 4 2009); echo $_
+5

grep/awk/perl, cal.

$ cal 4 2009
     April 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

(MarkusQ): , :

cal 4 2009 | tr ' ' '\n' | grep -v ^$ | tail -n 1
+3

, - , , . , , .

1 2009 .

+1

Source: https://habr.com/ru/post/1705722/


All Articles