2007年4月13日 星期五

chout: a small tool for debug. Hope you enjoy it. ^^


#!/bin/bash
# This software may be used and distributed according to the terms
# of the GNU General Public License (GPL), incorporated herein by reference.
# Drivers based on this skeleton fall under the GPL and must retain
# the authorship (implicit copyright) notice.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#filename: chout
#composer: Tick

if ! [[ "$CHOUT_DEBUG" ]];then
CHOUT_DEBUG=0
fi

function debug {
if (( $CHOUT_DEBUG!=0 ));then
echo [Chout Debug] $@
fi
}

function usage {
if (( $#==0 ));then
echo "USAGE: chout [-i] [- ] "
echo "Example: chout DEBUG <-- finding files contain DEBUG"
echo "Example: chout -name '*.pro' DEBUG <-- finding DEBUG from pro fil es"
echo "Example: chout -dir /tmp/ -type f DEBUG <-- all files in specific dire ctory"
echo "Example: chout -i DeBuG <-- ignore case"
echo "Other flags: -cmin -ctime"
echo "Please reference to \"man find\""
exit;
fi }

usage $@
ICASE=" "
DIR=" "
NAMEFLAG=" "
NAME=" "
TYPEFLAG=" "
TYPE=" "
CMINFLAG=" "
CMIN=" "
CTIMEFLAG=" "
CTIME=" "
ares=" "
while (( $# > 1 )) &&amp; ( [ "$1" == "-dir" ] || \
[ "$1" == "-i" ] || \
[ "$1" == "-name" ] || \
[ "$1" == "-type" ] || \
[ "$1" == "-ctime" ] || \
[ "$1" == "-cmin" ]);do

if [ "$1" == "-dir" ];then
shift
DIR="$1"
elif [ "$1" == "-type" ];then
shift
TYPEFLAG="-type"
TYPE="$1"
elif [ "$1" == "-name" ];then
shift
NAMEFLAG="-name"
NAME="$1"
elif [ "$1" == "-i" ];then
ICASE=" -i"
elif [ "$1" == "-ctime" ];then
shift
CTIMEFLAG="-ctime"
CTIME="$1"
elif [ "$1" == "-cmin" ];then
shift
CMINFLAG="-cmin"
CMIN="$1"
else
echo break time $@
break
fi
shift
done
usage $@

if [ "$ares" != " " ];then
echo -n ""
elif [ "$TYPEFLAG" == " " ] \
&& [ "$NAMEFLAG" == " " ] \
&& [ "$CTIMEFLAG" == " " ] \
&& [ "$CMINFLAG" == " " ] \
;then
debug DIR=$DIR
ares="`find ${DIR} -name '*.h'` `find ${DIR} -name '*.c'` `find ${DIR} -
name '*.cpp'`"
else
if [ "${NAME}" == " " ];then
ares=`find ${DIR} ${CTIMEFLAG} ${CTIME} ${CMINFLAG} ${CMIN} ${TYPEFLAG} ${TYPE}`
else
debug "find ${DIR} ${CTIMEFLAG} ${CTIME} ${CMINFLAG} ${CMIN} ${TYPEFLAG} ${TYPE} ${NAMEFLAG} \"${NAME}\""
ares=`find ${DIR} ${CTIMEFLAG} ${CTIME} ${CMINFLAG} ${CMIN} ${TYPEFLAG} ${TYPE} ${NAMEFLAG} "${NAME}"`
fi

fi

target=$1
shift

while (( $# > 0 ));do
target="$target "'*'"$1"
shift
done

debug "target is '$target'"

for arrs in $ares ;do

if (( `file $arrs | grep -e "text" -e "symbolic" | wc -l ` == 0 ));then
debug $arrs is binary
continue
fi
debug Try to parse $arrs
if [ -f $arrs ] && (( `cat $arrs | grep ${ICASE} "${target}" | wc -l` > 0 ));
then
echo "In file $arrs"
grep --color -n ${ICASE} "${target}" $arrs
#awk '{print NR":\t"$0}' $arrs | grep ${ICASE} $1 | sed -n "s/${target}/&/p"
echo " "
fi
done

沒有留言: