0

I cannot figure out why this function gets stuck.
What I am trying to do is create a folder from a predetermined start date up until the present month. Unfortunately it completes all the folder creations then gets stuck with the arithmetic posted below.
Would anyone be able to give me a hint to get on to the right track?
Here is my code:

yearloop(){
   year=$(echo "$firstmsgdate"|cut -d"/" -f3)
   zmmailbox -z -m $account gf /archive || zmmailbox -z -m $account cf /archive
   crntmnth=$(date +"%m")
   incmonth=0
   while [ $year -le $currentyear ]; do
     if [ "$year" -lt "$currentyear" ]; then
       #do stuff
       let year++
     elif [ $year -eq $currentyear ]; then
       while [ $incmonth -lt $crntmnth ]; do
         #do otherthings
         let incmonth++
       done
     else
       echo "Else"
     fi
   done
}

This is the output I receive
+ '[' 15 -le 15 ']'
+ '[' 15 -lt 15 ']'
+ '[' 15 -eq 15 ']'
+ '[' 2 -lt 02 ']'
+ '[' 15 -le 15 ']'
+ '[' 15 -lt 15 ']'
+ '[' 15 -eq 15 ']'
+ '[' 2 -lt 02 ']'
+ '[' 15 -le 15 ']'

2

When the year reaches current year, it's never incremented again (the elif is in effect), so the outer while loop which tests "less than or equal to" is still in effect. Put let year++ after the while loop in the elif part, and it will work. Or even better... call break explicitly at that point. It's never good to rely on the while loop to terminate on its own.

Additionally, you could simply use sequences and skip all the clumsy tests:

#initialization as before
let lastyear=currentyear-1
for year in $(seq $year $lastyear); do
   #do stuff with $year
done
#now months of the current year
for month in $(seq $incmonth $crntmnth); do
   #do stuff with $month and $currentyear
done

Your double loop was useless anyway - current year only happens once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.