我有500多个php文件,需要查找php文件的使用位置。所以我需要一个脚本或其他东西来帮助我在文件中找到这些用法/提及。我想写一个bash脚本来做这件事,但我不确定这是个好主意。你对此有什么想法吗?
命令行上有grep
:
grep -rnH --color=auto 'require "test.php"' SOURCE_FOLDER
该命令将打印每次出现的require "test.php"
以及文件名和行号(彩色)
您可以使用auto_append_file 将编写get_included_files()输出的脚本附加到文件中
这就是我最后所做的。这个bash脚本大体上解决了我的问题。仍然存在一个关于带有空格的文件名的问题。如果文件名中有空格,我会出错。现在对我来说没问题。
#!/bin/bash
DIR="/path/to/dir"
# save and change IFS
OLDIFS=$IFS
IFS=$''n'
# read all file name into an array
FILES=$(find $DIR -type f -name "*.php")
DFILES=$FILES
#General report file
ofile="general-report.txt"
#Unused files report file
nuffile="unused-files-report.txt"
#count unused files
yok=0
echo "Files have found. Proccessing..."
#Title for general report
echo "GENERAL REPORT FOR X " >> "$ofile"
#Title for unused files report
echo "UNUSED FILES FOR X" >> "$nuffile"
for i in $FILES
do
bulundu=0
dosyaadi=$(basename $i)
echo "###########################################################################################">> "$ofile"
echo "###########################################################################################"
echo "------>>>>> Looking for $dosyaadi ..."
echo "------>>>>> Looking for $dosyaadi ..." >> "$ofile"
for j in $DFILES
do
if grep -rnH --color=auto "$dosyaadi" "$j"
then
grep -rnH --color=auto "$dosyaadi" "$j" >> "$ofile"
bulundu=$((bulundu+1))
fi
done
if [ $bulundu -eq 0 ]
then
yok=$((yok+1))
echo "DOSYA $yok -->> $dosyaadi dosyasi hic bir php kodunda bulunamadi. Kullanilmiyor olmasi muhtemel.">> "$nuffile"
echo "Dosyanin konumu: $i">> "$nuffile"
echo -e ''n'n'>> "$nuffile"
fi
echo "__________________________________________________________________________________________">> "$ofile"
echo "__________________________________________________________________________________________"
echo "$dosyaadi dosyasinin diger dosyalarda $bulundu sayida eslesme bulundu.">> "$ofile"
echo "$dosyaadi dosyasinin diger dosyalarda $bulundu sayida eslesme bulundu."
echo "__________________________________________________________________________________________">> "$ofile"
echo "__________________________________________________________________________________________"
echo -e ''n'n'n'n'>> "$ofile"
echo -e ''n'n'n'n'
done
echo "TOPLAM $yok tane dosya kullanilmiyor gorunuyor!">> "$nuffile"
echo "Heyyo! Tüm tarama bitti $ofile dosyasına yazıldı."
# restore it
IFS=$OLDIFS