经常会碰到这样一个问题,知道某个函数的用法,但是想要看看它的源代码,知道它在某个目录下,却又不知道它在哪个文件里。众里寻他千百度,,,百度不行再google,,,
事实证明:然并卵,还是自己写一个函数,手动找找吧。
下面是用 Linux shell代码编写的程序searchfunindir.sh
#!/bin/bash
# author : Wangjie
# functions: search specific-string
# input : direction/file specific-string
# output: the line and relative info in files of direction including the specific-string
# 一个简单的查找函数,输入一个目录(或文件)和要查找的字符(可以是正则表达式)
# 输出查询的结果:文件所在目录、行号及相关信息
function searchstring(){
if [ -z $1 ] || [ -z $2 ];then #判断输入是否为空
echo "input error!"
return
fi
if [ ! -d $1 ];then # bu是一个目录
echo "$1 "
grep -n $2 $1
echo
echo
return
fi
#cd $1
direc=`ls $1`
for file in $direc # 遍历目录中文件
do
local newdir=${1}"/"${file} #完整路径
if [ -d $newdir ];then #是目录
searchstring $newdir $2
else # 是文件
local temp=`grep -c $2 $newdir` #是否包含要查找的字符串($2是第二个输入参数)
if [ "$temp" -gt 0 ];then
echo "$newdir "
grep -n $2 $newdir
echo
echo
fi
fi
done
}
#echo "call searchstring"
searchstring $1 $2
(1)查找字符串
(2)查找一个正则表达式
=========== 王杰 原创作品转载请注明出处==============