[SHELL]用于快速搜索C++/C/JAVA/汇编等源代码的SHELL脚本 博客分类: linux_app
面对动不动10多G容量的android源码目录,想要在这个源码目录里搜索一个 字符串,或者想要知道某个函数的定义,实在太难了,尤其是没有目的的搜索,速度是很慢的。但时间是弥足珍贵的,效率是必须提升的,我们总不能继续沦为更底层的码农吧,哈哈。这个脚本就是帮助大家从搜索源码的禁锢当中解脱的。
使用方法:
$ chgrep
please append 2 args behind the chgrep!
Help(Example):
chgrep <file_type> <find_in_sub_dir> <grep_string> [grep_options]
chgrep ch mediatek/ blacklight :is finding blacklight in *.c *.h files in mediatek dir
<file_type>:: ch: *.c,*.h; cpph ;cc;ch+k;java;asm; ccj: cpp,h,c,java; log: *.log *.txt; mk: mk,Makefile..;
示例:
chgrep ccj mediatek,hardware,system,frameworks/ "notifyPixelsChanged" -iw
上面这个示例的含义是,在当前目录下的 “mediatek,hardware,system,frameworks” 4个子目录中寻找 C/C++/JAVA代码,并在这些代码里查找 "notifyPixelsChanged" 字符串。
-i 的意思是 不区分大小写, -w的意思是 全字匹配。只要是 grep命令的参数,都可以用在这里。
比如 -n ,可以在搜索结果显示出找到关键词的行号。
脚本内容如下:
$ cat ../runsh/chgrep
#!/bin/bash
#curr_dir=$(pwd)/
ign_case=$4
#echo $curr_dir
sear_dirs=${2//','/' '}
echo Sear_dirs:$sear_dirs
search_dirs=($sear_dirs)
echo search_dirs:$search_dirs
if [ $# -lt 3 ]; then
echo "please append 2 args behind the chgrep!"
echo "Help(Example):"
echo "chgrep <file_type> <find_in_sub_dir> <grep_string>"
echo "chgrep ch mediatek/ blacklight :is finding blacklight in *.c *.h files in mediatek dir"
echo "<file_type>:: ch: *.c,*.h; cpph ;cc;ch+k;java;asm; ccj: cpp,h,c,java; log: *.log *.txt; mk: mk,Makefile..;"
else
echo Search_dir:$2
echo Search_Str:$3
echo Case_sensi:$4
for idx in $sear_dirs ; do
echo "----Searching dir: [ $idx ] for greping str: [ $3 ]"
if [ "$1" = "ch" ]; then
echo " Search c and h:"
find $curr_dir$idx \( -iname "*.c" -o -iname "*.h" \) | xargs grep $ign_case --color=auto "$3"
find $curr_dir$idx \( -iname "*.S" -o -iname "*.asm" \) | xargs grep $ign_case --color=auto "$3"
elif [ "$1" = "ch+k" ]; then
echo " Search c and h in dir(kernel,$2):"
find $curr_dir$idx \( -name "*.c" -o -name "*.h" \) | xargs grep $ign_case --color=auto "$3"
find $curr_dir$idx \( -iname "*.S" -o -iname "*.asm" \) | xargs grep $ign_case --color=auto "$3"
echo " ----------results in kernel:"
find kernel \( -name "*.c" -o -name "*.h" \) | xargs grep $ign_case --color=auto "$3"
find kernel \( -iname "*.S" -o -iname "*.asm" \) | xargs grep $ign_case --color=auto "$3"
elif [ "$1" = "cpph" ]; then
echo " Search cpp and h:"
find $curr_dir$idx \( -name "*.cpp" -o -name "*.h" \) | xargs grep $ign_case --color=auto "$3"
elif [ "$1" = "cc" ]; then
echo " Search cpp, h and c:"
find $curr_dir$idx -name "*.c" -o -name "*.cpp" -o -name "*.h" | xargs grep $ign_case --color=auto "$3"
elif [ "$1" = "java" ]; then
echo " Search java and aidl:"
find $curr_dir$idx \( -iname "*.java" -o -iname "*.aidl" \) | xargs grep $ign_case --color=auto "$3"
elif [ "$1" = "ccj" ]; then
echo " Search *.c and *.h:"
find $curr_dir$idx \( -name "*.c" -o -name "*.h" \) | xargs grep $ign_case --color=auto "$3"
echo " Search *.cpp and *.java *.aidl:"
find $curr_dir$idx -name "*.cpp" -o -name "*.java" -o -iname "*.aidl" | xargs grep $ign_case --color=auto "$3"
elif [ "$1" = "asm" ]; then
echo " Search assemble ,*.S,*.asm:"
find $curr_dir$idx -iname "*.S" -o -iname "*.asm" -o -iname "*.inc" | xargs grep $ign_case --color=auto "$3"
elif [ "$1" = "mk" ]; then
echo " Search makefile ,*.mk,*.mak, makefile, GNUmakefile, *.pl, *.py:"
find $curr_dir$idx \( -iname "*.pl" -o -iname "*.py" \) | xargs grep $ign_case --color=auto "$3"
find $curr_dir$idx \( -iname "*.mk" -o -iname "*.mak" \) | xargs grep $ign_case --color=auto "$3"
find $curr_dir$idx -iname "makefile" -o -iname "GNUmakefile" -o -iname "*config" -o -iname "*.rc"| xargs grep $ign_case --color=auto "$3"
elif [ "$1" = "log" ]; then
echo " Search *log, *.txt"
find $curr_dir$idx \( -iname "*.log" -o -iname "*.txt" \) | xargs grep $ign_case --color=auto "$3"
else
echo "--->Search default($1 files):"
find $curr_dir$idx -iname "$1" | xargs grep $ign_case --color=auto "$3"
fi
done
fi