#!/bin/bash #find_str.sh if [ $# -lt "2" ]; then echo "Usage: `basename $0` path name [option]" exit 1 fi #!-r表示递归处理子目录,-i表示忽略大小写 path=$1 name=$2 shift shift for option in "$@" do case $option in -r) dir_op="-r" ;; -i) lu_op="-i" ;; *) if [ -n "$option" ]; then echo "invalid option" exit 1 fi ;; esac done grep_str_of_file() { file=$1 str=$2 out=$(grep -n $lu_op "$str" "$file") if [ -n "$out" -a "$file" != "$0" ]; then echo "$file: $out" fi } find_str() { if [ -d "$1" ]; then for file in $1/* do if [ "$dir_op" = "-r" -a -d "$file" ]; then find_str $file $2 elif [ -f "$file" ]; then grep_str_of_file $file $2 fi done elif [ -f "$1" ]; then grep_str_of_file $1 $2 fi }