【发布时间】:2017-10-20 13:56:47
【问题描述】:
如果文本文件尚不存在,我需要将子目录中的所有文件pdftotext。我试过了:
find . -name "*.pdf" | while read file; if [ ! -e $file.txt ] do pdftotext $file; done;
但收到:-bash: 意外标记“完成”附近的语法错误
【问题讨论】:
标签: bash pdf while-loop find pdftotext
如果文本文件尚不存在,我需要将子目录中的所有文件pdftotext。我试过了:
find . -name "*.pdf" | while read file; if [ ! -e $file.txt ] do pdftotext $file; done;
但收到:-bash: 意外标记“完成”附近的语法错误
【问题讨论】:
标签: bash pdf while-loop find pdftotext
我建议:
find . -name "*.pdf" | while IFS= read -r file; do if [ ! -e "$file.txt" ]; then pdftotext "$file"; fi; done
请参阅:help while 和 help if
【讨论】:
不要将数据通过管道传输到 shell;从在 find 中执行一个shell 循环。
script='
for f in "$@"; do
if ! [ -e "$f" ]; then
pdftotext "$f"
fi
done
'
find . -name '*.pdf' -exec sh -c "$script" _ {} +
这适用于任何有效的文件名,即使是包含换行符的文件名。 find 将在每次调用时将尽可能多的文件传递给脚本,并根据需要多次调用脚本来处理所有文件。
【讨论】: