用shell脚本去掉源码的注释

转载地址:http://linux.chinaunix.net/techdoc/develop/2007/08/22/966038.shtml

 

该文件用来终极删除注释,操作系统限linux
#!/bin/bash
###the script use to delete comment
if [ -z "$1" ];then                  #检测是否输入了源文件
     echo "ipput the source file"
     exit 1
fi
postfix=`echo $1 |cut -f 2 -d '.'`   #取文件的后缀,以便根据不同的文件进行不同的处理
if [ -n "$2" ];then                  #存放处理源文件产生的信息的文件
     target_file=$2                  #如果给出了就直接调用
     touch ${target_file}
else
     prefix=`echo $1|cut -f 1 -d '.' ` #如果没有给出的话则生成一个和源文件名字一样,但后缀为temp的文件
     target_file=${prefix}.temp
     touch ${target_file}
fi
case "postfix" in   #####这句话不对:应该改成case "$postfix" in微笑
          sh )
              echo "it is shell script !"
              sed 's/[[:space:]]#.*//g' $1  |

              sed '/^#/d'                              |
              sed '/^[[:space:]]*$/d'          |

              sed '/^$/d' >${target_file}
              echo "the source file is $1,target file is ${target_file}"
              ;;
       c|java)
              echo "c  or java program"
              sed 's/\/\*.*\*\///g' $1             |

             sed '/\/\*/,/.*\*\//d'                    |
              sed 's/\/\/.*//g'                        |
              sed '/^[[:space:]]*$/d'           |

              sed '/^$/d' >${target_file}
              echo "the source file is $1,target file is ${target_file}"
              ;;
        *)
              echo "unknown file type !"
              rm -f ${target_file}
              ;;
esac

#话说这下面是什么意思啊,不是很懂呢   过段时间理解加写注释
#!/bin/sh -f
function del_comment_file()
{
        #C++模式注释的删除。
        #Delete the line begins with //
        #首先删除//在行首的行       
        sed -i '/^[ \t]*\/\//d' $file
        #Delete the line ends with //
        #注意已经除去了 //在行首的情况,下面匹配除去URL的//部分,因为代码中有一部分中有
        #URL,形如fun("ftp://")
        sed -i 's/\/\/[^"]*//' $file
        #删除以C语言中注释只在一行的行
        sed -i 's/\/\*.*\*\///' $file
        #Delete the lines between /* and */
        #删除C语言中注释跨行的情况
        sed -i '/^[ \t]*\/\*/,/.*\*\//d' $file
}
function del_comment()
{
#$Dir=`pwd`
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 Directory isn't exist."
        exit
fi
cd $Dir
del_comment

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/31547/showart_365267.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值