【问题标题】:bash 5 - continue while loop from a called functionbash 5 - 从被调用函数继续 while 循环
【发布时间】:2020-06-07 16:24:30
【问题描述】:

我最近从 bash 4.2 迁移到 5.0,不明白为什么从 while 循环调用此函数时不跳过

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循环调用函数

        # 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: ... 但不会继续下一次文件迭代

当 continue 不在函数调用中执行时,它会起作用

        # 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

感谢帮助

【问题讨论】:

    标签: bash


    【解决方案1】:

    这是bash 4.4 中的错误修复:

    xx。修复了可能允许从 shell 执行“中断”或“继续”的错误 影响在函数外运行的循环的函数。

    【讨论】:

    • 谢谢。请问我应该怎么做?我把编码作为一种爱好
    • 似乎通过return 1alreadyInQueue "$i" "1" || continue 找到了自己,再次感谢您为我指明了正确的方向
    • 对:退出状态是函数与其调用者通信的正确方式(也是一种好方式)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    相关资源
    最近更新 更多