Eeel
Eeel

Reputation: 127

bash 5 - continue while loop from a called function

I recenly moved from bash 4.2 to 5.0 and don't understand why this function don't skip when called from a while loop

alreadyInQueue () 
{
  # skip files if already in queue
  for task in "$HOME/.encode_queue/queue/"*
  do

      # Break if no task found
      [[ -f "$task" ]] || break

      # Sed line two from task file in queue (/dev/null error on empty queue)
      line=$( sed '2q;d' "$task" 2>/dev/null )

      # Store initial IFS
      OLD_IFS=$IFS

      # Extract tag and file from line
      IFS='|' read nothing tag file <<< "$line"

      # Restore IFS
      IFS=$OLD_IFS

      # Skip files already in queue with same preset (tag)
      if [[ "$tag" == "${tag_prst[x]}" && "$file" == "$1" ]]; then

      # Silent skip $2 argument: echo only if $2 = 1 
      [[ "$2" -eq "1" ]] && echo -e "\n** INFO ** Encode Queue, skip file already in queue:\n$i"

      # Continue n
      continue 2

      fi
  done 
}

while loop calling function

        # Find specified files
        find "$job_path" "${args_files[@]}" | sort | while read -r i
        do

        # Extracts var from $i
        fileSub

        # Skip files already in queue
        alreadyInQueue "$i" "1"

        echo "i should be skipped"

        done

script echo: ** INFO ** Encode Queue, skip file already in queue: ... but doesn't continue to next file iteration

When continue is not executed inside a function call it works

        # Find specified files
        find "$job_path" "${args_files[@]}" | sort | while read -r i
        do

        # Extracts var from $i
        fileSub

        # Skip files already in queue
        #alreadyInQueue "$i" "1"

        # skip files if already in queue waiting to be encoded
        for task in "$HOME/.encode_queue/queue/"*
        do

            # Break if no task found
            [[ -f "$task" ]] || break

            # Sed line two from task file in queue (/dev/null error on empty queue)
            line=$( sed '2q;d' "$task" 2>/dev/null )

            # Store initial IFS
            OLD_IFS=$IFS

            # Extract tag and file from line
            IFS='|' read nothing tag file <<< "$line"

            # Restore IFS
            IFS=$OLD_IFS

            # Skip files already in queue with same preset (tag)
            if [[ "$tag" == "${tag_prst[x]}" && "$file" == "$i" ]]; then

            # Silent skip $2 argument: echo only if $2 = 1 
            [[ "1" -eq "1" ]] && echo -e "\n** INFO ** Encode Queue, skip file already in queue:\n$i"

            # Continue n
            continue 2

            fi
        done

        echo "i should be skipped"

        done

help appreciated

Upvotes: 0

Views: 387

Answers (1)

chepner
chepner

Reputation: 530950

This was a bug fix made in bash 4.4:

xx. Fixed a bug that could allow `break' or `continue' executed from shell functions to affect loops running outside of the function.

Upvotes: 1

Related Questions