Bash: start date less than equal to end date Bash: start date less than equal to end date shell shell

Bash: start date less than equal to end date


With dateutils' datetest this is simple:

$ datetest 2014-11-1 --le 2016-01-1 ; echo $?0$ datetest 2014-11-1 --gt 2016-01-1 ; echo $?1

Then again, what you want is simply done by dateseq, which also happens to be a tool of the dateutils suite.

$ dateseq 2014-11-1 +1mo 2016-01-12014-11-012014-12-012015-01-012015-02-012015-03-012015-04-012015-05-012015-06-012015-07-012015-08-012015-09-012015-10-012015-11-012015-12-012016-01-01

Disclaimer: I am the author of the package.


-le is for numeric data. 2014-11-01 is not a number. Use < or >. (You need to escape them as \< or \>. Or use [[ instead of [.)

effectively, change

while [ "$m" != "$enddate" ]; do

to

until [ "$m" \> "$enddate" ]; do

or

until [ "$m" '>' "$enddate" ]; do

or

until [[ "$m" > "$enddate" ]]; do

Alternately, use seconds since epoch instead of ISO8601 format.

while [ "$(date -d "$m" +%s)" -le  "$(date -d "$enddate" +%s)" ]; do