I have a bash
script that performs commands on files based on dates contained in the file names. I'd like certain commands to run only for months greater than the current month, so I attempted to add an if
statement that compares the variables $day
(containing the numeric value of the month, taken from the for
loop's parameter) and $thismonth
(populated using date
).
For most values of month
the script works as expected, but when it loops through 08
and 09
it produces errors, e.g. value too great for base (error token is "08")
. Is there some difference between the values 08
and 09
and all the others...? Or what other mistake have I made?
Minimal example test.sh
:
Note: I'm a scripting novice so please feel free to correct any mistakes or inefficiencies even if not causing this particular issue. Also, parameters of the for
loop came from an answer to How to loop through dates using Bash?.
for d in 02{01..28} {04,06,09,11}{01..30} {01,03,05,07,08,10,12}{01..31}
do
day=${d: -2}
month=${d::2}
thismonth=$(date +%m)
if [[ "$month" -gt "$thismonth" ]]; then
echo day is $day
echo the month is $month
echo this month is $thismonth
echo Greater than this month
echo "" # to make output more easily readable
else
echo day is $day
echo the month is $month
echo this month is $thismonth
echo NOT greater than this month
echo "" # to make output more easily readable
fi
done
Expected results:
# for values of `month` between `02` and `12`
day is <value of $day>
the month is <value between 02 and 12>
this month is 01
Greater than this month
# when `month` is `01`:
day is <value of $day>
the month is 01
this month is 01
NOT greater than this month
Actual results are as expected for all values of month
except 08
and 09
. Excerpted output (repetitive lines removed):
day is 01
the month is 02
this month is 01
Greater than this month
[ . . . ]
day is 30
the month is 06
this month is 01
Greater than this month
./test.sh: line 7: [[: 09: value too great for base (error token is "09")
day is 01
the month is 09
this month is 01
NOT greater than this month
[ . . . ]
./test.sh: line 7: [[: 09: value too great for base (error token is "09")
day is 30
the month is 09
this month is 01
NOT greater than this month
day is 01
the month is 11
this month is 01
Greater than this month
[ . . . ]
day is 30
the month is 11
this month is 01
Greater than this month
day is 01
the month is 01
this month is 01
NOT greater than this month
[ . . . ]
day is 31
the month is 01
this month is 01
NOT greater than this month
day is 01
the month is 03
this month is 01
Greater than this month
[ . . . ]
day is 31
the month is 07
this month is 01
Greater than this month
./test.sh: line 7: [[: 08: value too great for base (error token is "08")
day is 01
the month is 08
this month is 01
NOT greater than this month
[ . . . ]
./test.sh: line 7: [[: 08: value too great for base (error token is "08")
day is 31
the month is 08
this month is 01
NOT greater than this month
day is 01
the month is 10
this month is 01
Greater than this month
[ . . . ]
day is 31
the month is 12
this month is 01
Greater than this month
This seemed especially surprising since these months are in different groups of the for
loop parameter (i.e. apparently unrelated to the fact that one loops through days 01
to 30
vs 01
to 31
so also not somehow related to number of days in thismonth
or something like that).
The full script (which checks for files named with dates formatted yyyymmdd
and removes certain those with certain dates) worked fine in the latter part of last year when I wanted to see results for all dates, but gave the above odd results when I added the if
statement in the minimal example in order to prevent output results for months greater than the current one (since no files yet exist for dates later than today).
bash --version
is GNU bash, version 5.1.4(1)-release
on Debian GNU/Linux 11 (bullseye)
(actually running Proxmox kernel where uname -srv
is Linux 5.15.39-4-pve #1 SMP PVE 5.15.39-4 (Mon, 08 Aug 2022 15:11:15 +0200)
in case that's somehow relevant).