网上看了一些帖子,删注释跑起来和要求都有些不符合,所以写了一个简单的脚本 ,去除.c .cpp中的注释
支持删除的注释有
1 单行注释
/*11111111111111*/
2 以//开头的注释
//222222222222
3以//结尾的注释
hello world //3333333333
不会删除hello world,只删除结尾的//33333
4 /**/ 多行注释
/*
*
*
/
或者这种类型
/**************
*
*
*
****************/
使用方式 ./commit.sh +绝对路径
下面附上代码
sed命令哪里加了’'空命令的原因是因为mac os 使用sed -i 出现sed: -i may not be used with stdin,网上帖子写的是前面加空命令就不会出错了
#!/bin/bash
echo "start del_commit"
read_dir(){
for file in `ls -a $1`
do
if [ -d $1"/"$file ];then
if [[ $file != '.' && $file != '..' ]];then
#遍历文件夹 输出处理的文件夹名字
read_dir $1"/"$file
fi
else
#处理.c .cpp后缀文件中的注释
if [ ${file##*.} = 'cpp' ] || [ ${file##*.} = 'c' ];then
#echo $file
#删除//开头的注释
sed -i '' '/^[ \t]*\/\//d' $1"/"$file
#删除//在行尾的注释
sed -i '' 's/\/\/[^"]*//' $1"/"$file
#删除/**/ 在一行的注释
sed -i '' 's/\/\*.*\*\///' $1"/"$file
#删除/**/ 跨行的注释
sed -i '' '/^[ \t]*\/\*/,/.*\*\//d' $1"/"$file
fi
fi
done
}
read_dir $1
echo "end del_commit"