gpt4 book ai didi

Linux 查找脚本结果未附加到输出文本文件

转载 作者:太空宇宙 更新时间:2023-11-04 12:05:34 29 4
gpt4 key购买 nike

我写了一个小的 shell 脚本,来识别我网站上的 PDF 文件关联页面。

它是逐个获取 pdf 源列表 url,作为输入并在网站内容中递归查找。

问题是当我运行脚本时发现结果没有附加到输出文件,但是当我使用 find 命令并手动在终端/腻子中运行时可以看到结果。

脚本:

#!/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
------------------------------------------

手动运行查找时,终端/腻子可以看到下面的结果。

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" '{}' \;

最佳答案

您是否尝试过 echo 中的 -e 选项来启用对反斜杠转义的解释?

另外,你为什么不简单地做 find | grep?

查找 ./-type f | grep "$name">> output_pdf_new.txt

在查找中尝试跟随(./ 而不是 .)

find ./ -type f -exec grep -l "$name" '{}' \; >>output_pdf_new.txt

关于Linux 查找脚本结果未附加到输出文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50849694/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com