date: invalid date ‘19820101+1 months’

Viewed 55

my bash code basically just needs to generate paths with dates as folder names. But gets stuck at 19820101 for some reason. Really cant quite figure out what's the special case with 1982? Any idea why this is happening?

DTZ=19540101
while [[ $DTZ -le 19850101 ]]
do
    echo username@servername:/path/filename_${DTZ}TO0300Z >> afile
    DTZ=$(date +%Y%m%d -d "$DTZ+1 months")
done
1 Answers

I can't repro, but try using a more explicit date format.

DTZ=19540101
while [[ $DTZ -le 19850101 ]]
do
    echo username@servername:/path/filename_${DTZ}TO0300Z >> afile
    DTZ=$(date +%Y%m%d -d "${DTZ:0:4}-${DTZ:4:2}-${DTZ:6:s} + 1 month")
done

The ${string:offset:length} parameter expansion is a Bash-only feature, but so is the [[ conditional you were already using.

Related