긴 이야기 짧게 나는 기능이 있습니다. 아래에서 "months_and_days"라고합니다. 그 목적은 하루에 24 개의 로그를 만들어 "년-월"디렉터리에 저장하는 것입니다.
아래에 "main"이라는 두 번째 함수가 있습니다. 기본은 12 개월 동안 간단한 for 루프를 기반으로 디렉토리를 만듭니다. 주 함수가 이러한 디렉터리를 만든 후 "months_and_days"함수를 호출하여 더미 로그 파일로 채 웁니다.
어떤 이유로 첫 달의 디렉토리 "2018-01"만 생성하고 채운 다음 중지합니다.
나는 전에 bash에서 스크립팅 한 적이 없기 때문에 months_and_days가 완료된 후 왜 루프를 완료하기 위해 주 함수로 돌아 가지 않는지 잘 모르겠습니다.
주 함수를 다시 호출하고 주 루프에 대한 전역 변수를 유지해야합니까?
아래 코드 :
months=12
testDir=/home/name/bashScripts/testDir
fileToCopyPath=/opt/logs/192.168.217.129/2019-11/1-192.168.217.129-2019-11-24-13.log
currentMonth=0
function months_and_days () {
declare -a daysArr=(31 28 31 30 31 30 31 31 30 31 30 31)
for ((i=1; i<=${daysArr[$1]}; i++))
do
for ((j=0; j<=23; j++ ))
do
if [ $j -le 9 ]
then
tar -czvf $testDir/2018-$currentMonth/1-192-168-217-129-2018-$currentMonth-$i-0$j.tar.gz $fileToCopyPath
else
tar -czvf $testDir/2018-$currentMonth/1-192-168-217-129-2018-$currentMonth-$i-$j.tar.gz $fileToCopyPath
fi
done
done
}
function main () {
for ((i=1; i<=$months; i++))
do
if [ $i -le 9 ]
then
mkdir $testDir/2018-0$i
chmod 775 $testDir/2018-0$i
currentMonth=0$i
months_and_days "$i-1"
else
mkdir $testDir/2018-$i
chmod 775 $testDir/2018-$i
currentMonth=$i
months_and_days "$i-1"
fi
done
}
main
i
main
및 months_and_days
함수 모두에서 변경되는 전역 변수입니다 . 따라서 months_and_days에 대한 첫 번째 호출 후 i의 값은 32 (1 월의 일 수보다 하나 더 많음)이므로 $ months보다 크므로 모든 것이 중단됩니다.
추가하다
local i
months_and_days의 첫 번째 줄로 수정합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다