摘要:指令窗口下阅读程序,且需要查找某个关键词、函数、变量在不同文件下的引用,可以利用此脚本查找。查找范围为当前路径以及其下各层子目录下的文件。
#! /bin/bash
find_word=$1
var=`ls`
function in_file
{
#echo "PC will step into directory is:$1, Current Path:`pwd`"
cd $1
#echo "Stepped into; then Path:`pwd`"
arr=`ls ` #可以进行筛选,如只需查看.cc和.h后缀文件内容 arr=`ls *.h *.cc`
for k in $arr
do
if [ -d $k ]
then
#echo "$k is directory"
in_file $k
else
#echo "$k is file"
grep $find_word -n $k
if [ $? == 0 ] ;then
echo "`pwd`:$k"
fi
fi
done
#echo "From Local Path:`pwd` --return up level"
cd ..
#echo "Return to:`pwd`"
}
for i in $var
do
#echo "Current Path:`pwd`"
if [ -d $i ]
then
#echo "$i is directory"
in_file $i
else
#echo "$i is file"
grep $find_word -n $i
if [ $? == 0 ];then
echo "`pwd`:$i"
fi
fi
done
取消echo前注释可以打印出文件夹跳转过程,文件庞大时建议注释echo,但if [ $? == 0 ]中的echo必须打印出来,不然即使找到查找内容,也不知道在哪个路径哪个文件。