[SHELL]用于快速搜索C++/C/JAVA/汇编等源代码的SHELL脚本

 

面对动不动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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值