You should quote all your variable expansions, and any other string that can contain whitespace or shell-reserved characters.
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
Also, this section (and others like it) has a problem:
Code:
strtmo=$(date --date='350 days ago' '+%m')
if [ "$strtmo" -eq 1 ]; then
startmo=0$(($strtmo - 1))
else
startmo=$(($strtmo -1))
fi
"
date +%m" outputs the month in the zero-padded format (01-12).
But bash treats numbers with leading zeros as
octal, so when your script reaches month 08, you're going to get a syntax error.
http://mywiki.wooledge.org/ArithmeticExpression
An easy fix here is simply to tell date to not zero-pad the output. Just add a "
-" in front of the formatting character. Just remember to correctly zero-pad the output yourself later, when and if you need it. You can use printf for zero-padding.
Also, in bash, numerical evaluations are better performed with
((..)), and for strings and complex tests it's recommended to use the new
[[..]] extended test keyword.
But actually, I think you can skip the test entirely and simplify the whole section into this:
Code:
strtmo=$( date --date='350 days ago' '+%-m' )
printf -v startmo "%02d" "$(( strtmo - 1 ))"
BTW, those two variable names (strtmo/startmo) are very similar, and probably easy to confuse. It's generally a good idea to use clear and unambiguous names wherever possible.
In fact, in this case, there really isn't even a need for two separate variables. Since you aren't using the initial value anywhere else in the script, why not just use a single name for the whole operation?
Code:
startmo=$( date --date='350 days ago' '+%-m' )
printf -v startmo "%02d" "$(( startmo - 1 ))"