マクロ切り替える

##########################################################################################
#         All copyright reserved!!
#Author     : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#Date       : 2012-09-14
#Description: Go to the requaired path recursively replacing your log function with given
#             macro "SMAPS_DEBUG_LOGMSG".
#             Enjoy it!!!
#History    : 2012_09_14 just complete Ver0.01
##########################################################################################
##########################################################################################
# function definition part
##########################################################################################
PrintHintYN()
{
    echo 'Please make sure the following two points before you run this script'
    echo '(1) This script has 'x' athority.'
    echo '(2) Usage: Logmsg_replace.sh Key_word Module_name Execute_dir'
    echo 'Example ./logmsg_replace.sh MEM_DEBUG CID_FBM ~/src/smaps_core/memory_manager'
    echo 'Are you sure now?[y/n]'
}


IsYourLogFunc()
{
    GREP_RESULT=`grep -w -l "$2" $1`
    if [ -z "${GREP_RESULT}" ] ; then
        echo "0";
    else
        echo "1";
    fi
}
##########################################################################################
# main process part
##########################################################################################
#Show the hint
PrintHintYN
read VAR_YN
case $VAR_YN in
    n|N     ) exit 0
              ;;
    y|Y     ) echo "Execution time starts at `date`"
              echo '------------------------------------------------------------------'
              ;;
    *       ) exit 0
              ;;
esac
# variable definition
KEYWORD=$1
echo "KEYWORD=${KEYWORD}"
MODULE=$2
echo "MODULE=${MODULE}"
DIR=$3
echo "DIR=${DIR}"
FILES=`find $3`
FILES=${FILES#smaps_debug_logmsg_replace_script.sh}
echo "FILES=${FILES}"
BACKUP=/tmp/${KEYWORD}.bak
echo "BACKUP=${BACKUP}"
DIFF=/tmp/${KEYWORD}.diff
echo "DIFF=${DIFF}"
BASENAME=
echo "BASENAME=${BASENAME}"
HOST=`whoami`
echo "HOST=${HOST}"
#
find ${DIR} -name "*" -print -exec grep -n ${KEYWORD} {} \; > ${BACKUP/%bak/bak1}
#
/bin/rm -rf /tmp/${HOST} >& /dev/null
mkdir /tmp/${HOST} >& /dev/null
#
for FILE in ${FILES} ; do
    if [ -f "${FILE}" ] ; then
        echo "${FILE} is file"
        BASENAME=`basename ${FILE}`
        #Judge if this file contains your log function
        FUNC_RTN=$(IsYourLogFunc ${FILE} ${KEYWORD})
        echo "\${FUNC_RTN}=${FUNC_RTN}"
        if [ "${FUNC_RTN}" -eq "1" ] ; then
            echo "/bin/sed -e 's/${KEYWORD}(/SMAPS_DEBUG_LOGMSG( ${MODULE}, LOG_DEBUGA,/g' $FILE > /tmp/${HOST}/${BASENAME}1"
            /bin/sed -e "s/${KEYWORD}(/SMAPS_DEBUG_LOGMSG( ${MODULE}, LOG_DEBUGA,/g" $FILE > /tmp/${HOST}/${BASENAME}1
            echo "/bin/sed -e 's/SMAPS_DEBUG_LOGMSG( ${MODULE}, LOG_DEBUGA, \"\([^\"]*\)\"/SMAPS_DEBUG_LOGMSG( ${MODULE}, LOG_DEBUGA, \"\1\/n\"/g' /tmp/${HOST}/${BASENAME}1 > /tmp/${HOST}/${BASENAME}2"
            /bin/sed -e "s/SMAPS_DEBUG_LOGMSG( ${MODULE}, LOG_DEBUGA, \"\([^\"]*\)\"/SMAPS_DEBUG_LOGMSG( ${MODULE}, LOG_DEBUGA, \"\1\/n\"/g" /tmp/${HOST}/${BASENAME}1 > /tmp/${HOST}/${BASENAME}2
            echo "/bin/rm /tmp/${HOST}/${BASENAME}1"
            /bin/rm /tmp/${HOST}/${BASENAME}1
            echo "/bin/rm ${FILE}"
            /bin/rm ${FILE}
            echo "sed '/#include/a\#include "common_facility/debug_logger/smapsDebug.h"' /tmp/${HOST}/${BASENAME}2 | grep -n "common_facility/debug_logger/smapsDebug.h" | awk -F: '{print $1}'"
            LINE=`grep -n "#include" /tmp/${HOST}/${BASENAME}2 | awk -F: '{print $1}'`
            echo "LINE=$LINE"
            LAST_LINE=`echo ${LINE} | awk '{print $NF}'`
            echo "LAST_LINE=$LAST_LINE"
            echo "sed -e ${LAST_LINE},${LAST_LINE} a\#include \"common_facility/debug_logger/smapsDebug.h\" /tmp/${HOST}/${BASENAME}2 > /tmp/${HOST}/${BASENAME}.sed"
            sed -e "${LAST_LINE},${LAST_LINE} a\#include \"common_facility/debug_logger/smapsDebug.h\"" /tmp/${HOST}/${BASENAME}2 > /tmp/${HOST}/${BASENAME}.sed
            echo "/bin/rm /tmp/${HOST}/${BASENAME}2"
            /bin/rm /tmp/${HOST}/${BASENAME}2
            echo "cp /tmp/${HOST}/${BASENAME}.sed /tmp/${HOST}/${BASENAME}2"
            cp /tmp/${HOST}/${BASENAME}.sed /tmp/${HOST}/${BASENAME}2
            echo "cp /tmp/${HOST}/${BASENAME}2 ${FILE}"
            cp /tmp/${HOST}/${BASENAME}2 ${FILE}
        fi #IsYourLogFunc
    fi #is file
done
#Delete all temp files
echo "/bin/rm -rf /tmp/${HOST} >& /dev/null"
/bin/rm -rf /tmp/${HOST} >& /dev/null
#
find ${DIR} -name "*" -print -exec grep -n "SMAPS_DEBUG_LOGMSG" {} \; > ${BACKUP/%bak/bak2}
#
#diff -rcb ${BACKUP/%bak/bak1} ${BACKUP/%bak/bak2} > ${DIFF}
vimdiff ${BACKUP/%bak/bak1} ${BACKUP/%bak/bak2}
echo "As to the diff result, please refer to ${DIFF}"
#Return to shell commandline
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值