我正在尝试编写两个(编辑:shell)脚本并且遇到了一些困难。我将解释目的,然后提供脚本和当前输出。
1:递归获取目录中每个文件名的列表。然后在该目录中的所有文件的内容中搜索每个文件名。应返回特定文件名每次出现的路径、文件名和行号。
2:递归获取目录中每个文件名的列表。然后为每个文件名搜索目录中所有文件的内容。应该返回在目录中的任何文件中都没有找到的每个文件的路径和文件名。
我最终想使用脚本 2 来查找和删除(实际上将它们移动到另一个目录进行归档)网站中未使用的文件。然后我想使用脚本 1 查看每次出现并过滤任何重复的文件名。
我知道我可以让脚本 2 在运行时移动每个文件,而不是作为第二步,但我想在执行任何操作之前确认脚本功能是否正确。我会在确认它运行正常后对其进行修改。
我目前正在 strqsh 中的 IMBi 系统上对此进行测试。
我的测试文件夹结构是:
scriptTest
---subDir1
------file4.txt
------file5.txt
------file6.txt
---subDir2
------file1.txt
------file7.txt
------file8.txt
------file9.txt
---file1.txt
---file2.txt
---file3.txt
我在一些包含现有文件名的文件中有文本。
这是我当前的脚本 1:
#!/bin/bash
files=`find /www/Test/htdocs/DLTest/scriptTest/ ! -type d -exec basename {} \;`
for i in $files
do
grep -rin $i "/www/Test/htdocs/DLTest/scriptTest" >> testReport.txt;
done
现在它可以正常工作,但提供匹配文件的路径除外。grep 默认不返回文件路径吗?
我离脚本 2 有点远:
#!/bin/bash
files=`find /www/Test/htdocs/DLTest/scriptTest/ ! -type d`
for i in $files
do
#split $i on '/' and store into an array
IFS='/' read -a array <<< "$i"
#get last element of the array
echo "${array[-1]}"
#perform a grep similar to script 2 and store it into a variable
filename="grep -rin $i "/www/Test/htdocs/DLTest/scriptTest" >> testReport.txt;"
#Check if the variable has anything in it
if [ $filename = "" ]
#if not then output $i for the full path of the current needle.
then echo $i;
fi
done
我不知道如何将字符串$i
拆分为数组。我在第 6 行不断收到错误消息
001-0059 Syntax error on line 6: token redirection not expected.
我打算在一个实际的 linux 发行版上尝试这个,看看我是否得到不同的结果。
我很欣赏任何先进的见解。