【发布时间】:2018-06-14 04:37:21
【问题描述】:
我编写了一个小的 shell 脚本,用于识别我网站中的 PDF 文件关联页面。
将pdf源列表url逐一作为输入,在网站内容中递归查找。
问题是当我运行脚本查找结果未附加到输出文件时, 但是当我使用 find 命令并在终端/putty 中手动运行时可以看到结果。
脚本:
#!/bin/bash
filename="PDF_Search_File.txt"
while read -r line
do
name="$line"
echo "*******pdf******** - $name\n" >>output_pdf_new.txt
find . -type f -exec grep -l "$name" '{}' \; >>output_pdf_new.txt
echo "*******pdf******** - $name\n" >>output_pdf_new.txt
done < "$filename"
源列表 url 输入文件 (PDF_Search_File.txt)
/static/pdf/pdf1.pdf
/static/pdf/pdf2.pdf
/static/pdf/pdf3.pdf
--------------------
输出结果文件(output_pdf_new.txt)
./Search_pdf.sh
*******pdf******** - /static/pdf/pdf1.pdf\n
*******pdf******** - /static/pdf/pdf1.pdf\n
./Search_pdf.sh
*******pdf******** - /static/pdf/pdf2.pdf\n
*******pdf******** - /static/pdf/pdf2.pdf\n
./Search_pdf.sh
*******pdf******** - /static/pdf/pdf3.pdf\n
*******pdf******** - /static/pdf/pdf3.pdf\n
------------------------------------------
终端/putty 可以在手动运行查找时看到下面的结果。
find . -type f -exec grep -l "/static/pdf/pdf1.pdf" '{}' \;
./en/toyes/zzz/index.xhtml
./en/toyes/kkk/index.xhtml
--------------
但脚本有问题,只输出回显结果如上输出结果。
更新 当我用 bash -x 执行脚本时,它给出了以下结果
[user@server1 generated_content]# bash -x Search_pdf.sh
+ filename=PDF_Search_File.txt
+ read -r line
+ name=$'/static/pdf/pdf1.pdf\r'
\n'cho '*******pdf******** - /static/pdf/pdf1.pdf
+ find . -type f -exec grep -l $'/static/pdf/pdf1.pdf\r' '{}' ';'
\n'cho '*******pdf******** - /static/pdf/pdf1.pdf
+ read -r line
+ name=$'/static/pdf/pdf2.pdf\r'
\n'cho '*******pdf******** - /static/pdf/pdf2.pdf
+ find . -type f -exec grep -l $'/static/pdf/pdf2.pdf\r' '{}' ';'
这里有问题
+ find . -type f -exec grep -l $'/static/pdf/pdf2.pdf\r' '{}' ';'
find 命令应该是下面这样,但是在执行的时候就和上面一样了
find . -type f -exec grep -l "/static/pdf/pdf1.pdf" '{}' \;
【问题讨论】:
-
运行脚本时和手动运行命令时是否在同一目录下?
-
是的,我在同一个目录中。我也在同一位置运行脚本
-
把
set -x放在脚本的开头,这样你就可以看到所有正在执行的命令,也许你会在那里看到一些错误。 -
将
ls ./en/toyes/*/index.xhtml放入脚本中。它显示了什么? -
停止使用 Windows 为 Unix 编辑文件。