0

Long story short I have a function. Its called "months_and_days" down below. Its purpose is to create 24 logs per day and put them in a "year-month" directory.

I have a second function called "main" down below. Main create directories based on a simple for loop for 12 months. After the main function creates those directories it calls the "months_and_days" function to populate it with dummy log files.

For some reason, it only creates and populates the first months' directory "2018-01" and then stops...

I've never scripted in bash before so I'm not sure why after months_and_days finishes, it doesnt go back to the main function to complete it's loop.

Do I have to call the main function again and keep a global variable for the main loop?

Code below:

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
2

i is a global variable which is being altered in both the main and months_and_days functions. So after the first call to months_and_days the value of i is 32 (one more than the number of days in jan) so is greater than $months, so things stop.

Add a

local i

as the first line in months_and_days to fix it.

| improve this answer | |
  • Atta be Icarus. Thank you for pointing out that I forgot the basic fundamentals of lexical scoping lol. Sometimes when learning new scripting or language I get so caught up in the syntax I forgot the basics lol – noobcoderiam Nov 25 '19 at 23:22
  • @noobcoderiam the point is that bash is dynamically scoped rather than lexically scoped. See comments in unix.stackexchange.com/questions/551291/… for a longer discussion where a professional compiler writer tells me I am wrong. – icarus Nov 25 '19 at 23:28

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.