[bash]: 删除代码注释

#!/bin/bash
 
function del_comment_file()
{
    #delete the comment line begin with '//comment'
    sed -i "/^[ \t]*\/\//d" $file    #i选项表示直接对文件而不是副本操作
   
    #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   #取cd后的参数进行循环
        case $file in      
        *.c)                   #如果是.c文件,就直接调用
            del_comment_file
            ;;
        *.cpp)               #如果是.cpp文件,也直接调用
            del_comment_file
            ;;
        *.h)                   #如果是.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//"`  #将文件名中的前边部分全部用空换掉,s是替换的意思
        del_comment_file
    else
        del_comment_file
    fi
 
    exit 0;
fi
 
if [ -d $DIR ]; then     #如果是目录
    cd $DIR                 #打开目录,然后进入目录进行处理调用
    del_comment  
    exit 0;
fi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值