0

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

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you. how am i supposed to do it the right way please ? i'm codding as a hobby
seems to figured out myself with return 1 and alreadyInQueue "$i" "1" || continue thanks again for pointing me in the right direction
Right: the exit status is a correct way (and a good way) for the function to communicate with its caller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.