Bash error: value too great for base
Consider the following:
#!/bin/bash
month='08'
for m in $( seq 1 $(( ${month} - 1 )) ); do
rm "backupfile-${m}.tar.bz2"
done
When run, it produces the following error:
$ /bin/bash ./test.sh
test.sh: line 6: 08: value too great for base (error token is "08")
According to man 1 bash
:
Constants with a leading 0 are interpreted as octal numbers.
Solution 1: force the number to be interpreted as decimal:
#!/bin/bash
month='08'
for m in $( seq 1 $(( 10#${month} - 1 )) ); do
rm "backupfile-${m}.tar.bz2"
done
Solution 2: strip the leading zero:
#!/bin/bash
month='08'
for m in $( seq 1 $(( ${month#0} - 1 )) ); do
rm "backupfile-${m}.tar.bz2"
done
(or if there are multiple leading zeros:)
#!/bin/bash
month='08'
for m in $( seq 1 $(( $(echo "${month}" | sed 's/^0*//') - 1 )) ); do
rm "backupfile-${m}.tar.bz2"
done