findcode shell script.

来源:互联网 发布:vb编程设计计算器 编辑:程序博客网 时间:2024/06/13 16:37

#!/bin/bash

 

if [ "$1" = "" ]; then

    echo "Syntax:"

    echo " $(basename $0) [-q] [-i] [-b] [-t] [-n] [-c] [-p path] code"

    echo " -q: only print file names"

    echo " -i: case insensitive"

    echo " -b: look also in build dirs: '*-linux-gnu/...'" 

    echo " -t: don't look in '3pp/...'" 

    echo " -n: print matching line number."

    echo " -c: shows matched string in color (should not be used when piping output)" 

    exit

fi

 

path=.

 

grep_cmd="/bin/grep"

 

while (( "$#" )); do

    if [ "$1" == "-q" ] ; then

        quiet="-l"

    fi

    if [ "$1" == "-i" ] ; then

        ignore_case="-i"

    fi

    if [ "$1" == "-b" ] ; then

        ### This will destroy the pruning of *-linux-gnu and 

        ### therefore those directories will be checked:

        check_build_dirs="true"

    fi

    if [ "$1" == "-t" ] ; then

        skip_3pp="-name 3pp -prune -or"

    fi

    if [ "$1" == "-p" ] ; then

        shift;

        path="$1"

    fi

    if [ "$1" == "-n" ]; then

        numbering="-n"

    fi

    if [ "$1" == "-c" ]; then

        grep_cmd="/bin/grep --color=auto"

    fi

    code=$1

    shift;

done

 

find $path -name "dist" -prune -or -name ".svn" -prune -or \

-name "*-linux-gnu$check_build_dirs" -prune -or \

-name "vip*00$check_build_dirs" -prune -or \

-name "st40$check_build_dirs" -prune -or \

-name "bcm74xx$check_build_dirs" -prune -or $skip_3pp \

-iname "*.c" -or -iname "*.h" -or -iname "*.cpp" -or \

-name "Makefile*" -or -name "Imakefile" -or -name "dist_*" -or \

-name "dist-*" -or -name "*.idl" -or -name "*.htm*" -or \

-name "*.xml" -or -name "*.sh" -or -name "*.perl" -or \

-name "*.pl" -or -name "configure" -or -name "*.in" -or \

-name "*.pm" -or -name "*.js" -or -name "*.java" -or \

-name "*.patch" -or -name "PATCHES" -or -name "*.txt" -or \

-name "*.py" -or -name "series" -or -name "*.mk" \

| sed -e "s/ /\\\ /g" | xargs $grep_cmd $numbering $quiet $ignore_case "$code"




说明:

       sed -e "s/ /\\\ /g"   对本行内容进行替换, 全局范围内 ”空格“替换为“\ "

        xargs grep 对每行执行grep命令;





leo_20120827

#!/bin/sh


if [ -z "$1" ]; then

    echo "Usage:"

    echo " $(basename $0) [-q] [-i] [-b] [-t] [-n] [-c] [-p path] code"

    echo " -b: look also in build dirs: '*-linux-gnu/...'"

    echo " -c: force color output"

    echo " -i: case insensitive"

    echo " -n: print matching line number"

    echo " -q: only print file names"

    echo " -t: don't look in '3pp/...'"

    exit

fi


path=.

color="--color=auto"

grep_cmd="/bin/grep"



while [ "$#" -gt 0 ]; do

    if [ "$1" = "-q" ] ; then

        quiet="-l"

    fi

    if [ "$1" = "-i" ] ; then

        ignore_case="-i"

    fi

    if [ "$1" = "-b" ] ; then

        # This will destroy the pruning of *-linux-gnu and therefore those

        # directories will be checked:

        check_build_dirs="true"

    fi

    if [ "$1" = "-t" ] ; then

        skip_3pp="-name 3pp -prune -or"

    fi

    if [ "$1" = "-p" ] ; then

        shift

        path="$1"

    fi

    if [ "$1" = "-n" ]; then

        numbering="-n"

    fi

    if [ "$1" = "-c" ]; then

        color="--color=always"

    fi

    code=$1

    shift

done


find $path \( \

  $skip_3pp \

  -name ".pc" -prune -or \

  -name "*-linux-gnu$check_build_dirs" -prune -or \

  -name ".svn" -prune -or \

  -name "bcm74xx$check_build_dirs" -prune -or \

  -name "dist" -prune -or \

  -name "host$check_build_dirs" -prune -or \

  -name "st40$check_build_dirs" -prune -or \

  -name "vip*00$check_build_dirs" -prune \) -or \( \

  -iname "*.c" -or \

  -iname "*.cpp" -or \

  -iname "*.h" -or \

  -iname "*.hpp" -or \

  -name "*.htm*" -or \

  -name "*.idl" -or \

  -name "*.in" -or \

  -name "*.java" -or \

  -name "*.js" -or \

  -name "*.mk" -or \

  -name "*.patch" -or \

  -name "*.perl" -or \

  -name "*.php" -or \

  -name "*.pl" -or \

  -name "*.pm" -or \

  -name "*.py" -or \

  -name "*.sh" -or \

  -name "*.tpl" -or \

  -name "*.txt" -or \

  -name "*.xml" -or \

  -name "Imakefile" -or \

  -name "Makefile*" -or \

  -name "PATCHES" -or \

  -name "configure" -or \

  -name "dist-*" -or \

  -name "dist_*" -or \

  -name "series" \) \

 -print0  \

| xargs -0 $grep_cmd -H $color $numbering $quiet $ignore_case -- "$code"






20120827

#!/bin/sh


if [ -z "$1" ]; then

    echo "Usage:"

    echo " $(basename $0) [-q] [-i] [-b] [-t] [-n] [-c] [-p path] code"

    echo " -b: look also in build dirs: '*-linux-gnu/...'"

    echo " -c: force color output"

    echo " -i: case insensitive"

    echo " -n: print matching line number"

    echo " -q: only print file names"

    echo " -t: don't look in '3pp/...'"

    exit

fi


path=.

color="--color=auto"

grep_cmd="/bin/grep"



while [ "$#" -gt 0 ]; do

    if [ "$1" = "-q" ] ; then

        quiet="-l"

    fi

    if [ "$1" = "-i" ] ; then

        ignore_case="-i"

    fi

    if [ "$1" = "-b" ] ; then

        # This will destroy the pruning of *-linux-gnu and therefore those

        # directories will be checked:

        check_build_dirs="true"

    fi

    if [ "$1" = "-t" ] ; then

        skip_3pp="-name 3pp -prune -or"

    fi

    if [ "$1" = "-p" ] ; then

        shift

        path="$1"

    fi

    if [ "$1" = "-n" ]; then

        numbering="-n"

    fi

    if [ "$1" = "-c" ]; then

        color="--color=always"

    fi

    code=$1

    shift

done


find $path \( \

  $skip_3pp \

  -name "*-linux-gnu$check_build_dirs" -prune -or \

  -name ".svn" -prune -or \

  -name "bcm74xx$check_build_dirs" -prune -or \

  -name "dist" -prune -or \

  -name "host$check_build_dirs" -prune -or \

  -name "st40$check_build_dirs" -prune -or \

  -name "vip*00$check_build_dirs" -prune -or \

  -iname "*.c" -or \

  -iname "*.cpp" -or \

  -iname "*.h" -or \

  -iname "*.hpp" -or \

  -name "*.htm*" -or \

  -name "*.idl" -or \

  -name "*.in" -or \

  -name "*.java" -or \

  -name "*.js" -or \

  -name "*.mk" -or \

  -name "*.patch" -or \

  -name "*.perl" -or \

  -name "*.php" -or \

  -name "*.pl" -or \

  -name "*.pm" -or \

  -name "*.py" -or \

  -name "*.sh" -or \

  -name "*.tpl" -or \

  -name "*.txt" -or \

  -name "*.xml" -or \

  -name "Imakefile" -or \

  -name "Makefile*" -or \

  -name "PATCHES" -or \

  -name "configure" -or \

  -name "dist-*" -or \

  -name "dist_*" -or \

  -name "series" \

\) -print0 \

| xargs -0 $grep_cmd -H $color $numbering $quiet $ignore_case -- "$code"


0 0
原创粉丝点击