博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[bash]: 删除代码注释
阅读量:2191 次
发布时间:2019-05-02

本文共 1117 字,大约阅读时间需要 3 分钟。

#!/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

转载地址:http://pvdub.baihongyu.com/

你可能感兴趣的文章
程序员--学习之路--技巧
查看>>
解决问题之 MySQL慢查询日志设置
查看>>
contOS6 部署 lnmp、FTP、composer、ThinkPHP5、docker详细步骤
查看>>
TP5.1模板布局中遇到的坑,配置完不生效解决办法
查看>>
PHPstudy中遇到的坑No input file specified,以及传到linux环境下遇到的坑,模板文件不存在
查看>>
TP5.1事务操作和TP5事务回滚操作多表
查看>>
composer install或composer update 或 composer require phpoffice/phpexcel 失败解决办法
查看>>
TP5.1项目从windows的Apache服务迁移到linux的Nginx服务需要注意几点。
查看>>
win10安装软件 打开时报错 找不到 msvcp120.dll
查看>>
PHPunit+Xdebug代码覆盖率以及遇到的问题汇总
查看>>
PHPUnit安装及使用
查看>>
PHP项目用xhprof性能分析(安装及应用实例)
查看>>
composer安装YII
查看>>
Sublime text3快捷键演示
查看>>
sublime text3 快捷键修改
查看>>
关于PHP几点建议
查看>>
硬盘的接口、协议
查看>>
VLAN与子网划分区别
查看>>
Cisco Packet Tracer教程
查看>>
02. 交换机的基本配置和管理
查看>>