代码之家  ›  专栏  ›  技术社区  ›  Guy Sadoun

无法读取bash管道中的所有文件行

  •  1
  • Guy Sadoun  · 技术社区  · 6 年前

    我找了又找不到任何东西,也许我不能很好地理解这个问题。 我有一个bash函数,它读取当前目录和子目录中的文件,我试图排列文本并分析数据,但是如果我使用管道,不知何故我会丢失行。 代码:

    function recursiveFindReq {
        for file in *.request; do
            if [[ -f "$file" ]]; then
              echo handling "$file"
              echo ---------------with pipe-----------------------
              cat "$file" | while read -a line; do  
                if (( ${#line} > 1 )); then
                    echo ${line[*]}
                fi
              done
              echo ----------------without pipe----------------------
              cat "$file"
              echo
              echo num of lines: `cat "$file" | wc -l`
              echo --------------------------------------
            fi
        done
    
        for dir in ./*; do
            if [[ -d "$dir" ]]; then
                echo cd to $dir
                cd "$dir"
                recursiveFindReq "$1"
                cd ..
            fi
        done
    }
    

    输出为: losing lines even when they meet requirements

    我用两个红色箭头标出了我丢失信息的地方

    0 回复  |  直到 6 年前