
이 기능이 왜 막히는지 이해가 되지 않습니다.
내가하고 싶은 것은 예정된 시작일부터 당월까지의 폴더를 만드는 것입니다. 불행하게도 모든 폴더 생성이 완료되고 아래에 게시된 연산이 중단됩니다.
누구든지 내가 올바른 길로 갈 수 있도록 힌트를 줄 수 있나요?
이것은 내 코드입니다.
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
}
이것은 내가 받는 출력입니다.
+ '[' 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 -乐15 ']'
답변1
연도가 현재 연도에 도달하면 다시 증가하지 않으므로(유효하게 elif
) while
"작거나 같음"을 테스트하는 외부 루프가 계속 작동합니다. 부품의 루프 뒤에 넣으면 let year++
작동합니다. 아니면 더 나은 방법은... 이 시점에서 명시적으로 호출하는 것입니다. 종속성 루프가 자체적으로 종료되는 것은 결코 좋지 않습니다.while
elif
break
while
또한 간단하게 시퀀스를 사용하고 모든 어색한 테스트를 건너뛸 수 있습니다.
#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
어쨌든 이중 루프는 쓸모가 없습니다. 올해 딱 한 번만 발생합니다.