用shell脚本去掉源码的注释
2011-09-12 22:08
495 查看
转载地址: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
该文件用来终极删除注释,操作系统限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
相关文章推荐
- 利用Shell脚本去掉C/C++源码中的注释
- 使用shell脚本统计源码文件中的注释行数.(// , /**/)
- shell脚本中实现大段代码的注释方法
- Shell脚本注释及变量定义
- jsp原理分析 jsp声明和jsp脚本讲解 源码分析 及jsp注释讲解 jsp页面的访问量
- 脚本游戏之四: 坦克大战源码注释(待续。。。)
- Windows下Android源码下载脚本(shell+awk)
- shell脚本监控网站状态(附注释)
- 使用shc保护shell脚本源码
- shell脚本注释模版
- 【Shell脚本学习11】Shell注释
- Linux下shell脚本的块注释
- 批量替换类名Shell脚本源码解析
- Android源码下载,利用一个shell脚本
- shell脚本学习5——字符串和注释
- shell脚本批量注释的方法
- Shell脚本源码编译安装MySQL5.1.73
- Shell脚本注释写法
- 关于shell脚本里的单行注释和多行注释方法
- 下载Google官方/CM Android源码自己主动又一次開始的Shell脚本