Please use
[code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
If I may make a suggestion, clean, consistent formatting makes code readable and more easily debuggable. Indent all your sub-commands, and separate logical sections with whitespace. Add comments anywhere the code isn't completely obvious (and remember, what seems obvious to you now will not be a year or so down the line).
Since environment variables are generally all upper-case, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.
I also personally recommend putting your
then and
do keywords on the same line as your
if/
for/
while/
until keywords. it helps keep the clutter down and visually separate the sub-commands.
I would format the code you posted above (with fix) like this:
Code:
thismonth=$(date +%B)
thisday=$(date +%d)
lastmonth=$(date -d "-1 month" +%B)
#if the current day is 01, thismonth needs to be reset to lastmonth
if [[ "$thisday" = "01" ]]; then
thismonth=$lastmonth
echo "$thismonth = $lastmonth"
fi
I also suggest getting into the habit of quoting ALL of your variable substitutions. You should never leave the quotes off a variable expansion unless you explicitly want the resulting string to be word-split by the shell.
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
This is particularly important when using the old "
[" test, which can break if it sees the wrong number of arguments. The newer "
[[" test keyword isn't as vulnerable to this kind of thing, and has other benefits as well, so you should really be using it for your
bash scripts instead anyway.
http://mywiki.wooledge.org/BashFAQ/031