开始学写shell
磕磕碰碰的写完了一个预处理的脚本,很简单,用来对代码里面的#ifdef DEBUG … #else … #endif来做处理 如果脚本没带参数 就认为启用DEBUG 带参数就认为不启用DEBUG 弱的很 暴汗……
其实以前也写过shell脚本,简单的比如切换有线和无线,复杂的比如解决rar x的中文问题(不过新版本的rar貌似没这个问题了),但还是好多东东要学@.@ 还得在实践中一点一点的被lisp给侵蚀着 呵呵
#!/bin/bash
#
# Author: JayXie
# Date: 2007-01-23
# Email: oxwjo@163.com
#
E_BADARGS=65
E_NOFILE=66
E_NODIR=67
DEBUG_MACRO=1
STATUS=”NONE”
if [ $# -gt 0 ]
then
DEBUG_MACRO=0
fi
while read -r line
do
case $STATUS in
NONE)
IS_DEF=`echo $line | awk ‘{if($1~/\#ifdef/) print $2}’`
if [ ! -z $IS_DEF ] && [ $IS_DEF = "DEBUG" ]; then
if [ $DEBUG_MACRO -gt 0 ]; then
STATUS=”IF_OUT”
else
STATUS=”IF_RETAIN”
fi
else
echo “$line”
fi
;;
IF_OUT)
IS_ELSE=`echo $line | awk ‘{if($1~/\#else/) print “true”}’`
IS_END=`echo $line | awk ‘{if($1~/\#endif/) print “true”}’`
if [ ! -z $IS_ELSE ] && [ $IS_ELSE = "true" ]; then
STATUS=”ELSE_RETAIN”
elif [ ! -z $IS_END ] && [ $IS_END = "true" ]; then
STATUS=”NONE”
else
echo “$line”
fi
;;
ELSE_OUT)
IS_END=`echo $line | awk ‘{if($1~/\#endif/) print “true”}’`
if [ ! -z $IS_END ] && [ $IS_END = "true" ]; then
STATUS=”NONE”
else
echo “$line”
fi
;;
IF_RETAIN)
IS_ELSE=`echo $line | awk ‘{if($1~/\#else/) print “true”}’`
IS_END=`echo $line | awk ‘{if($1~/\#endif/) print “true”}’`
if [ ! -z $IS_ELSE ] && [ $IS_ELSE = "true" ]; then
STATUS=”ELSE_OUT”
elif [ ! -z $IS_END ] && [ $IS_END = "true" ]; then
STATUS=”NONE”
fi
;;
ELSE_RETAIN)
IS_END=`echo $line | awk ‘{if($1~/\#endif/) print “true”}’`
if [ ! -z $IS_END ] && [ $IS_END = "true" ]; then
STATUS=”NONE”
fi
;;
esac
done
