如何重构一个发现 | xargs 一个衬里到人类可读的代码

How to refactor a find | xargs one liner to a human readable code

我已经为 tesseract 和 abbyyocr11 编写了一个 OCR 包装器批处理和服务脚本,可以在此处找到:https://github.com/deajan/pmOCR

主要功能是一个 find 命令,它使用 -print0 将其参数传递给 xargs 以处理特殊的文件名。 find 命令变得越来越复杂,最终成为一个非常长的单行代码,变得难以维护:

find "$DIRECTORY_TO_PROCESS" -type f -iregex ".*\.$FILES_TO_PROCES" ! -name "$find_excludes" -print0 | xargs -0 -I {} bash -c 'export file="{}"; function proceed { eval "\"'"$OCR_ENGINE_EXEC"'\" '"$OCR_ENGINE_INPUT_ARG"' \"$file\" '"$OCR_ENGINE_ARGS"' '"$OCR_ENGINE_OUTPUT_ARG"' \"${file%.*}'"$FILENAME_ADDITION""$FILENAME_SUFFIX$FILE_EXTENSION"'\" && if [ '"$_BATCH_RUN"' -eq 1 ] && [ '"$_SILENT"' -ne 1 ];then echo \"Processed $file\"; fi && echo -e \"$(date) - Processed $file\" >> '"$LOG_FILE"' && if [ '"$DELETE_ORIGINAL"' == \"yes\" ]; then rm -f \"$file\"; fi"; }; if [ "'$CHECK_PDF'" == "yes" ]; then if ! pdffonts "$file" 2>&1 | grep "yes" > /dev/null; then proceed; else echo "$(date) - Skipping file $file already containing text." >> '"$LOG_FILE"'; fi; else proceed; fi'

有没有更好的方法将查找结果传递给人类可读的函数(不会影响太多速度)?

谢谢。

不要使用 bash -c。您已经承诺从 find 命令为每个文件启动一个新的 bash 进程,因此只需将代码保存到一个文件中,然后 运行 使用

find "$DIRECTORY_TO_PROCESS" -type f -iregex ".*\.$FILES_TO_PROCES" \
     ! -name "$find_excludes" -print0 |
     xargs -0 -I {} bash script.bash {}

OK,创建脚本,然后运行找到。

#!/bin/bash

trap cleanup EXIT
cleanup() { rm "$script"; }

script=$(mktemp)
cat <<'END' > "$script"
########################################################################
file=""

function proceed { 
    "$OCR_ENGINE_EXEC" "$OCR_ENGINE_INPUT_ARG" "$file" "$OCR_ENGINE_ARGS" "$OCR_ENGINE_OUTPUT_ARG" "${file%.*}$FILENAME_ADDITION$FILENAME_SUFFIX$FILE_EXTENSION"
    if [ "$_BATCH_RUN" -eq 1 ] && [ "$_SILENT" -ne 1 ]; then 
        echo "Processed $file"
    fi
    echo -e "$(date) - Processed $file" >> "$LOG_FILE"
    if [ "$DELETE_ORIGINAL" == "yes" ]; then 
        rm -f "$file"
    fi
}

if [ "$CHECK_PDF" == "yes" ]; then 
    if ! pdffonts "$file" 2>&1 | grep "yes" > /dev/null; then 
        proceed
    else 
        echo "$(date) - Skipping file $file already containing text." >> '"$LOG_FILE"'; 
    fi
else 
    proceed
fi
########################################################################
END

find "$DIRECTORY_TO_PROCESS" -type f \
                             -iregex ".*\.$FILES_TO_PROCES" \
                           ! -name "$find_excludes" \
                             -exec bash "$script" '{}' \;

引用了heredoc的'END',所以变量在脚本真正执行之前不会展开

您可以完全替换 find。在 bash 4(我将在此处展示)中更容易,但在 bash 3.

中可行
proceed () {
  ...
}

shopt -s globstar

extensions=(pdf tif tiff jpg jpeg bmp pcx dcx)
for ext in "${extensions[@]}"; do
  for file in /some/path/**/*."$ext"; do
    [[ ! -f $file || $file = *_ocr.pdf ]] && continue
    # Rest of script here
  done
done

bash 4 之前,您可以编写自己的递归函数来降低目录层次结构。

descend () {
    for fd in ""/*; do
        if [[ -d $fd ]]; then
            descend "$fd"
        elif [[ ! -f $fd || $fd != *."$ext" || $fd = *_ocr.pdf ]]; then
            continue
        else
            # Rest of script here
        fi
     done
 }

 for ext in "${extensions[@]}"; do
     descend /some/path "$ext"
 done

我用替换的查找命令完成了 while 循环,即:

while IFS= read -r -d $'[=10=]' file; do
        if ! lsof -f -- "$file" > /dev/null 2>&1; then
            if [ "$_BATCH_RUN" == true ]; then
                Logger "Preparing to process [$file]." "NOTICE"
            fi
            OCR "$file" "$fileExtension" "$ocrEngineArgs" "$csvHack"
        else
            if [ "$_BATCH_RUN" == true ]; then
                Logger "Cannot process file [$file] currently in use." "ALWAYS"
            else
                Logger "Deferring file [$file] currently being written to." "ALWAYS"
                kill -USR1 $SCRIPT_PID
            fi
        fi
    done < <(find "$directoryToProcess" -type f -iregex ".*\.$FILES_TO_PROCES" ! -name "$findExcludes" -and ! -wholename "$moveSuccessExclude" -and ! -wholename "$moveFailureExclude" -and ! -name "$failedFindExcludes" -print0)

while 循环从 file 变量中的 find 命令读取每个文件。 在 while 中使用 -d $'\0' 并在 find 命令中使用 -print0 有助于处理特殊文件名。