1. 第一种方法,使用ls
#!/bin/bash
function getdir(){
for element in `ls $1`
do
dir_or_file=$1"/"$element
if [ -d $dir_or_file ]
then
getdir $dir_or_file $2
else
empty=`echo $dir_or_file|grep '.php$'`
if [ x$empty != 'x' ]
then
#echo $dir_or_file
cat $dir_or_file|grep $2
fi
fi
done
}
root_dir="/home/"
str="hello"
getdir $root_dir $str
2. 第二种方法,使用find
#!/bin/bash
function getdir(){
for file in `find $1 -name *.php`
do
cat $file|grep $2
done
}
root_dir="/home/"
str="hello"
getdir $root_dir $str