在查找指定目录下文件中的字符串的shell脚本
在linux终端界面中用find与grep命令结合,查找代码中的宏定义或字符串变量,有时需要很长的命令长,不堪其扰。
于是写了一个简单的脚本,简化查找的命令。
使用说明:
1、因为是用grep实现的查找,所以支持grep支持的正则表达式;
2、使用-m或--macro选项可以查找宏定义;
3、不指定目录则从当前目录开始查找。
# 宏定义或字符串变量查找
# link:www.jquerycn.cn
# date:2013/2/27
Usage()
{
echo "Usage: ${0##*/} [option] [path] search-content"
echo " -S,--string search string"
echo " -m,--macro search macro define"
echo " -t,--typedef search typedef statement"
echo " -c,--class search class declare"
echo " -s,--struct search class declare"
echo " -e,--extended-regexp support grep extended regular expression"
echo " -i,--ignore-case ignore search-content case"
echo " -h,--help display help information"
echo "[Note]: If you don't specify path, default searching from current directory."
}
if [ $# -eq 0 ]; then
Usage
exit 1
fi
while [ $# -gt 0 ]; do
case $1 in
-*)
case $1 in
-S|--string) # this is the default option if you don't specify any option
SEARCH_TYPE="string";;
-m|--macro)
SEARCH_TYPE="macro";;
-t|--typedef)
SEARCH_TYPE="typedef";;
-c|--class)
SEARCH_TYPE="class";;
-s|--struct)
SEARCH_TYPE="struct";;
-E|--extended-regexp)
GREP_EXT_REG_OPTION="-E";;
-i|--ignore-case)
GREP_IGNORE_CASE_OPTION="-i";;
-h|--help)
Usage
exit 0;;
*)
echo "Invalid usage: unknown option $1!"
exit 1;;
esac ;;
*)
if [ -d "$1" ]; then
if [ -z "$SEARCH_PATH" ]; then
SEARCH_PATH="$1"
elif [ -z "$SEARCH_CONTENT" ]; then
SEARCH_CONTENT="$1"
else
echo "Invalid usage: too many directory parameters!"
exit 1
fi
elif [ -z "$SEARCH_CONTENT" ]; then
SEARCH_CONTENT="$1"
else
echo "Invalid usage: too many search-content parameters!"
exit 1
fi ;;
esac
shift
done
GREP_EXTRAL_OPTION="-n -H --color $GREP_EXT_REG_OPTION $GREP_IGNORE_CASE_OPTION"
if [ -z $SEARCH_TYPE ]; then
SEARCH_TYPE="string"
fi
if [ -z "$SEARCH_CONTENT" ]; then
# perhaps the content to be searched is a path-style name, and not specify path parameter
if [ -n "$SEARCH_PATH" ]; then
SEARCH_CONTENT="$SEARCH_PATH"
SEARCH_PATH="."
else
echo "Invalid usage: no search-content parameter!"
exit 1
fi
fi
if [ -z "$SEARCH_PATH" ]; then
SEARCH_PATH="."
fi
SearchString()
{
for file in `ls`; do
if [ "$file" = "." -o "$file" = ".." ]; then
continue
fi
if [ -d $file ]; then
cd ./$file # add "./" for those special directories whose name begin with "-"
SearchString
cd ..
continue
fi
if grep $GREP_EXTRAL_OPTION "$SEARCH_CONTENT" "`pwd`/$file" ; then
let "SEARCH_MATCH_COUNT+=1"
fi
done
}
Search()
{
for file in `ls`; do
if [ "$file" = "." -o "$file" = ".." ]; then
continue
fi
if [ -d $file ]; then
cd $file
Search
cd ..
continue
fi
case $SEARCH_TYPE in
macro)
if grep $GREP_EXTRAL_OPTION "#[[:space:]]*define[[:space:]]*$SEARCH_CONTENT" "`pwd`/$file" ; then
let "SEARCH_MATCH_COUNT+=1"
fi;;
typedef)
if grep $GREP_EXTRAL_OPTION "typedef.*$SEARCH_CONTENT" "`pwd`/$file" ; then
let "SEARCH_MATCH_COUNT+=1"
fi;;
class)
if grep $GREP_EXTRAL_OPTION "class.*$SEARCH_CONTENT" "`pwd`/$file" ; then
let "SEARCH_MATCH_COUNT+=1"
fi;;
struct)
if grep $GREP_EXTRAL_OPTION "struct.*$SEARCH_CONTENT" "`pwd`/$file" ; then
let "SEARCH_MATCH_COUNT+=1"
fi;;
*)
if grep $GREP_EXTRAL_OPTION "$SEARCH_CONTENT" "`pwd`/$file" ; then
let "SEARCH_MATCH_COUNT+=1"
fi;;
esac
done
}
pushd $SEARCH_PATH > /dev/null
SEARCH_MATCH_COUNT=0
if [ "$SEARCH_TYPE" = "string" ]; then
SearchString
else
Search
fi
if [ $SEARCH_MATCH_COUNT -lt 1 ]; then
echo -e "No $SEARCH_TYPE matches \033[40;31m$SEARCH_CONTENT\033[0m in \033[40;31m$SEARCH_PATH\033[0m directory and its sub-directory!"
fi
popd > /dev/null
查找文件小工具:
Usage()
{
echo "Usage: ${0##*/} [option] [path] filename"
echo " -a,--absolute dispaly absolute path"
echo " -r,--relative dispaly relative path"
echo " -h,--help display this information"
echo "[Note]: If you don't specify any option, -a will be used by default."
}
if [ $# -eq 0 ]; then
echo "Invalid usage: no filename specified!"
exit 1
fi
while [ $# -gt 0 ]; do
case $1 in
-*)
# ignore duplicated and conflicted options
case $1 in
-a|--absolute)
FLAG_ABSOLUTE_PATH="true"
;;
-r|--relative)
FLAG_ABSOLUTE_PATH="false"
;;
-h|--help)
Usage
exit 0
;;
*)
echo "Invalid usage: unknown option $1!"
exit 1
;;
esac ;;
*)
if [ -d "$1" ]; then
if [ -z "$SEARCH_PATH" ]; then
SEARCH_PATH="$1"
elif [ -z "$SEARCH_FILE" ]; then
SEARCH_FILE="$1"
else
echo "Invalid usage: too many directory parameters!"
exit 1
fi
elif [ -z "$SEARCH_FILE" ]; then
SEARCH_FILE="$1"
else
echo "Invalid use: too many filename parameters!"
exit 1
fi
;;
esac
shift
done
if [ -z $FLAG_ABSOLUTE_PATH ]; then
FLAG_ABSOLUTE_PATH="true"
fi
if [ -z "$SEARCH_FILE" ]; then
# perhaps the filename to be searched is a path-style name, and not specify path parameter
if [ -n "$SEARCH_PATH" ]; then
SEARCH_FILE="$SEARCH_PATH"
SEARCH_PATH="."
else
echo "Invalid usage: no filename specified!"
exit 1
fi
fi
if [ -z "$SEARCH_PATH" ]; then
SEARCH_PATH="."
fi
SearchFile()
{
# In some systems, find not support -L option (find: invalid predicate '-L')
if find -L /usr -maxdepth 1 -name tmp 2&> /dev/null ; then
FUN_SF_FIND_L_OPTION="-L"
else
FUN_SF_FIND_L_OPTION=""
fi
if [ "$FLAG_ABSOLUTE_PATH" = "true" ]; then
cd $SEARCH_PATH
FUN_SF_CURRENT_PATH=`pwd`/
find $FUN_SF_FIND_L_OPTION . -type f | grep $SEARCH_FILE | sed "s,\./,$FUN_SF_CURRENT_PATH,g" | grep --color $SEARCH_FILE
else
find $FUN_SF_FIND_L_OPTION $SEARCH_PATH -type f | grep --color $SEARCH_FILE
fi
}
SearchFile
您可能感兴趣的文章:
通过find命令寻找文件并拷贝到一个指定目录的方法
(原创)shell查找符号链接及其指向目标的方法介绍
有关B-SHELL的一些运用方法
几个shell自动化脚本(定期清理、磁盘空间、搜寻关键字)
删除除了某个文件以外的所有文件
Linux egrep命令
shell if 命令参数说明
bash shell脚本执行的几种方法
inux shell初级入门教程
shell脚本中if语句中判断参数详解