Search code examples
bashreturn

Cannot understand why our function calls return twice?


We have a 15 year (or so) old script we are trying to figure out and document. We have found some errors in it but one specific log file gives us much headache. and I would love some help figuring it out. First the function that are run with the question:

#=========================================================#
# Define function removeOldBackupFile.                    #
#=========================================================#
removeOldBackupFile()
{
#set -x
  echo "Removing old backups if they exists." >> "${report}"
  local RCLOC=0
  spaceBefore=$(getAvailableSpace ${backupDirectory})
  timesToWait=60 # Wait a maximum of 10 minutes before bailing

  cat ${oldDbContainer} | while read fileName
  do
    echo "Old file exists. Removing ${fileName}." >> "${report}"
    removeFileIfExist "${fileName}"
    RC=$?
    echo "Resultcode for removing old backup is: RC=$RC." >> "${report}"
    RCLOC=$(($RC+$RCLOC))
    spaceAfter=$(getAvailableSpace ${backupDirectory})

    # Wait for the OS to register that the file is removed
    cnt=0
    while [ $spaceAfter -le $spaceBefore ]; do
      cnt=$((cnt+1))
      if [ $cnt -gt $timesToWait ]; then
        echo "Waited too long for space in ${backupDirectory}" | tee -a "${report}"
        RCLOC=$(($RCLOC+1)) 
        return $RCLOC
      fi
      sleep 10 
      spaceAfter=$(getAvailableSpace ${backupDirectory})
    done
  done

  return $RCLOC
}

The place where this function is ran looks as follows:

#=========================================================#
# Remove old backupfiles if any exist.                    #
#=========================================================#
  removeOldBackupFile
  RC=$?
  RCSUM=$(($RC+$RCSUM))

We have identified that the if condition is a bit wrong and the while loops would not work as intended if there are multiple files.

But what bothers us is output from a log file:

...
+ cnt=61
+ '[' 61 -gt 60 ']'
+ echo 'Waited too long for space in /<redacted>/backup'
+ tee -a /tmp/maintenanceBackupMessage.70927
Waited too long for space in /<redacted>/backup
+ RCLOC=1
+ return 1
+ return 0
+ RC=0
+ RCSUM=0
...

As seen in the log output after the inner loop have ran 60 times and ending it returns 1 as expected.. BUT! it also have return 0 after!? Why is it also returning 0?

We are unable to figure out the double returns... Any help appriciated


Solution

  • The first return executes in the subshell started by the pipe cat ${oldDbContainer} | while .... The second return is from return $RCLOC at the end of the function. Get rid of the useless use of cat:

    removeOldBackupFile()
    {
    #set -x
      echo "Removing old backups if they exists." >> "${report}"
      local RCLOC=0
      spaceBefore=$(getAvailableSpace ${backupDirectory})
      timesToWait=60 # Wait a maximum of 10 minutes before bailing
    
      while read fileName
      do
        ...
      done < ${oldDbContainer}
    
      return $RCLOC
    }