删除c/c++中注释的shell脚本
时间:2007-07-27 来源:djfreeman
下面这个脚本实现了删除c/c++ source code中注释的功能。它接收一个命令行参数,该命令行参数或者是一个c/c++源文件,或者是一个目录。当参数是一个目录时,该脚本删除该目录下及子目录下所有源文件中的注释。
脚本源代码如下:
#!/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 [ -d $DIR ]; then
cd $DIR
del_comment
fi
if [ -f $DIR ]; then
file=`basename $DIR`
cd `echo $DIR|sed -e "s/$file//"`
del_comment_file
fi
脚本源代码如下:
#!/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 [ -d $DIR ]; then
cd $DIR
del_comment
fi
if [ -f $DIR ]; then
file=`basename $DIR`
cd `echo $DIR|sed -e "s/$file//"`
del_comment_file
fi
相关阅读 更多 +