下面这个脚本实现了删除c/c++ source code中注释的功能。它接收一个命令行参数,该命令行参数或者是一个c/c++源文件,或者是一个目录。当参数是一个目录时,该脚本删除该目录下及子目录下所有源文件中的注释。
delcomment.sh
------------------------------------
#!/bin/bash
#delcomment.sh
#function: this shell script delete the comment in c/c++ source file
function del_comment_file()
{
#delete the comment line begin with '//comment'
sed -i "/^[ \t]*\/\//d" $file
#delete the commnet line end with '//comment'
sed -i "s/\/\/[^\"]*//" $file
#delete the comment only occupied one line '/* commnet */'
sed -i "s/\/\*.*\*\///" $file
#delete the comment that occupied many lines '/*comment
# *comment
# */
sed -i "/^[ \t]*\/\*/,/.*\*\//d" $file
}
function del_comment()
{
for file in `ls `; do
case $file in
*.c)
del_comment_file
;;
*.cpp)
del_comment_file
;;
*.h)
del_comment_file
;;
*)
if [ -d $file ]; then
cd $file
del_comment
cd ..
fi
;;
esac
done
}
DIR=$1
if [ ! -e $DIR ]; then
echo "The file or directory does not exist."
exit 1;
fi
if [ -f $DIR ]; then
file=`basename $DIR`
if [[ `echo $DIR | grep /` == $DIR ]]; then
cd `echo $DIR | sed -e "s/$file//"`
del_comment_file
else
del_comment_file
fi
exit 0;
fi
if [ -d $DIR ]; then
cd $DIR
del_comment
exit 0;
fi
------------------------------------
# delcomment.sh arg
注: 脚本delcomment.sh的参数可以:
(1) 当前目录下的文件;
(2) 目录;
(3) 带路径的文件。
(4) 原文 http://hi.baidu.com/zengzhaonong/blog/item/e85dbcccd844771300e92846.html
1 条评论:
看样子您没有考虑字符串中的注释吧~
发表评论