Search code examples
linuxbashtokenoctal

Getting a Syntax error with an expression containing 10#


#!/bin/bash
startdate=2009-02-21
enddate=2009-11-30
var=$startdate
while true
do
echo $var
touch $var.txt

#it's line 9
[[ "10#$var" -ge "$enddate" ]] || break

var=$(date +%Y-%m-%d --date "$var +1 day")
done

It returns the error

line 9:value too great for base (error token is "08")

Evidently, the error is in the expression 10#, but I can't think of a different way how to write the string, besides the ones I've already tried.


Solution

  • Convert your dates into seconds then do the comparison, this will be a better approach -

    startdate=$(date -d 2009-02-21 +%s)
    enddate=$(date -d 2009-11-30 +%s)
    ...
    [[ "$startdate" -ge "$enddate" ]] || break
    ...