Infinite looping in script

we have one script which we use to send mail in our environment. If we are giving correct attachment script runs fine but if we give a attachment name which is not present on server then this script go to infinite loop and causing all memory to be used. could any one please suggest me what is wrong

below is the portion of script where attachment condition is defined.

#==========================================
#  Validate the passed arguments
#

attach_total_size=0
n=0
while [[ $n -lt ${#PASSED_ATTACHMENTS[*]} ]];
do
   if [[ ! -r ${PASSED_ATTACHMENTS[$n]} ]];
   then
      invalid_attachments="$invalid_attachments ${PASSED_ATTACHMENTS[$n]}"
      continue
   else
      size=$(ls -al ${PASSED_ATTACHMENTS[$n]} | awk '{print $5}')
      (( attach_total_size = attach_total_size + size ))
   fi
   (( n = n + 1 ))
done

# Check if the size of overall attachments doesn't exceed Limit (2MB=2097152Byte)
if [[ $attach_total_size -gt $ATTACH_SIZE_LIMIT ]] ;
then
   msg "Attachment(s) not included due to the size larger than 2MB."
   ret_val2=$ERR_ATTACHMENT
elif [[ -n ${invalid_attachments} ]];
then
   msg "Attachment(s), ${invalid_attachments}, not found. Aborting."
   return $ERR_INVALID_VALUE
fi

when we use wrong attachment (attachment is not present) then scripts goes to infinite loop. below is the O/P of debugging mode. in below example "/home/auto/anshu/attccc" is the file name which is not present on server.

+ attach_total_size=0
+ n=0
+ [[ 0 -lt 1 ]]
+ [[ ! -r /home/auto/anshu/attccc ]]
+ invalid_attachments=' /home/auto/anshu/attccc'
+ continue
+ [[ 0 -lt 1 ]]
+ [[ ! -r /home/auto/anshu/attccc ]]
+ invalid_attachments=' /home/auto/anshu/attccc /home/auto/anshu/attccc'
+ continue
+ [[ 0 -lt 1 ]]
+ [[ ! -r /home/auto/anshu/attccc ]]
+ invalid_attachments=' /home/auto/anshu/attccc /home/auto/anshu/attccc /home/auto/anshu/attccc'
+ continue
+ [[ 0 -lt 1 ]]
+ [[ ! -r /home/auto/anshu/attccc ]]
+ invalid_attachments=' /home/auto/anshu/attccc /home/auto/anshu/attccc /home/auto/anshu/attccc /home/auto/anshu/attccc'
+ continue

Delete the

continue
1 Like

Thanks .. it worked fine :slight_smile: