首页 | 资讯动态 | linux基础 | 系统管理 | 网络管理 | 编程开发 | linux数据库 | 服务器技术 | linux相关 | linux认证 | 嵌入式 | 下载中心 | 专题 | linux招聘 | HR | 镜像
OKLinux中文技术站
·设为首页
·加入收藏
·联系我们
系统管理: 中文环境 系统管理 桌面应用 内核技术 | Linux基础: 基础入门 安装配置 常用命令 经验技巧 软件应用 | Linux数据库: Mysql Postgre Oracle DB2 Sybase other
网络管理: 网络安全 网络应用 Linux服务器 环境配置 黑客安全 | 编程开发: PHP CC++ Python Perl Shell 嵌入式开发 java jsp | PHP技术: PHP基础 PHP技巧 PHP应用 PHP文摘
搜索中心 Linux招聘 Linux专题 Apache | Linux相关: 硬件相关 Linux解决方案 Linux认证 企业应用 其它Unix | 相关下载: 资料下载 参考手册 开发工具 服务器类 软路由 其它
 技术搜索:
会员中心 注册会员 高级搜索  
  → 当前位置:首页>编程开发>shell>正文

Linux中使用Shell脚本去掉源码注释的方法

http://www.oklinux.cn  2007-07-27  赛迪网 sixth  会员收藏  游客收藏  【 】 
您查看的文章来源于http://www.oklinux.cn

该文件用来终极删除注释,操作系统限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
          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

上一篇:在Ubuntu Linux系统英文界面下处理中文   下一篇:Linux下如何使用OpenSSH配置端口转发

收藏于收藏夹】 【评论】 【推荐】 【打印】 【关闭
相关文档
·Shell脚本调试技术
·Linux解释器原理详细介绍
·Shell脚本 实现vi/cp/mv前自动备份源文件
·Shell脚本显示当前网络连接与对应物理地址
·Linux操作系统下使用yum升级的详细方法
·UNIX和Linux Shell正则表达式语法介绍
·重新定义Expect在Cron中的交互信息输出
·Shell 计算明天和昨天日期的函数
·如何提高Import命令的性能
·Shell Script(bash)--教学例
·Linux 下对于passwd 命令的使用详解
·VI高级命令集锦
·Linux命令Man解释:chsh :更换你登入时所用的shell
·用AIX的shell进行数学计算
·AIX操作系统shell的参数
·在AIX操作系统下shell的变量
发表评论
密码: 匿名评论
评论内容:

(不超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规)
 
  最新文档
·Shell编程基础:单引号和双引号含义区
·Linux下的定时器:alarm()与setitimer(
· Linux和DOS的FORMAT命令区别[参考篇
· linux指令大全[2][注]来源于天津Lin
· linux指令大全[1][注]来源于天津Lin
· linux指令大全[3][注]来源于天津Lin
· 最常用的基本操作命令[推荐篇]
· 文件和目录操作相关命令详解
· 文件内容查询命令
· 文件内容统计命令
· 文件的复制、删除和移动命令
· 目录的创建与删除命令
  阅读排行
·shell编程实例三:一个简单的目录菜单
·Linux shell 脚本实例一
·awk中使用的shell命令-浅记
·常用Linux Shell技巧集锦
·linux shell 变量和运算符<变量详解>
·文件管理 chgrp命令详解
· 新手看招 Linux Shell编程简单一例
· bash字符串匹配的用法
·shell编程-循环语句
·UNIX和Linux Shell正则表达式语法介绍
·Shell编程笔记(6)-脚本参数传递
·Linux 下对于passwd 命令的使用详解
·mount命令里的bind选项
· Bash For Beginner's Guide (Bash新手
· 文件链接命令ln
网摘收藏: