Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1038759
  • 博文数量: 836
  • 博客积分: 43880
  • 博客等级: 大将
  • 技术积分: 5485
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-20 17:36
文章分类

全部博文(836)

文章存档

2011年(1)

2008年(835)

我的朋友

分类: LINUX

2008-08-20 18:24:39


该文件用来终极删除注释,操作系统限linux
#!/bin/bash
###thescriptusetodeletecomment

if[-z"$1"];then#检测是否输入了源文件
echo"ipputthesourcefile"
exit1
fi

postfix=`echo$1|cut-f2-d''.''`#取文件的后缀,以便根据不同的文件进行不同的处理

if[-n"$2"];then#存放处理源文件产生的信息的文件
target_file=$2#如果给出了就直接调用
touch${target_file}
else
prefix=`echo$1|cut-f1-d''.''`#如果没有给出的话则生成一个和源文件名字一样,但后缀为temp的文件
target_file=${prefix}.temp
touch${target_file}
fi

case"postfix"in
sh)
echo"itisshellscript!"
sed''s/[[:space:]]#.*//g''$1|sed''/^#/d''|\
sed''/^[[:space:]]*$/d''|sed''/^$/d''>${target_file}
echo"thesourcefileis$1,targetfileis${target_file}"
;;
c|java)
echo"corjavaprogram"
sed''s/\/\*.*\*\///g''$1|sed''/\/\*/,/.*\*\//d''|\
sed''s/\/\/.*//g''|\
sed''/^[[:space:]]*$/d''|sed''/^$/d''>${target_file}
echo"thesourcefileis$1,targetfileis${target_file}"
;;
*)
echo"unknownfiletype!"
rm-f${target_file}
;;
esac


//-------------------------

#!/bin/sh-f

functiondel_comment_file()
{
#C  模式注释的删除。
#Deletethelinebeginswith//
#首先删除//在行首的行
sed-i''/^[\t]*\/\//d''$file

#Deletethelineendswith//
#注意已经除去了//在行首的情况,下面匹配除去URL的//部分,因为代码中有一部分中有
#URL,形如fun("ftp://")
sed-i''s/\/\/[^"]*//''$file

#删除以C语言中注释只在一行的行
sed-i''s/\/\*.*\*\///''$file

#Deletethelinesbetween/*and*/
#删除C语言中注释跨行的情况
sed-i''/^[\t]*\/\*/,/.*\*\//d''$file
}


functiondel_comment()
{
#$Dir=`pwd`

forfilein`ls`;do
case$filein
*.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"TheDirectoryisn''texist."
exit
fi

cd$Dir

del_comment


下载本文示例代码
阅读(315) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~