设为首页 加入收藏

TOP

Linux下让bash脚本中利用sed制作自己的类模板(一)
2014-11-23 22:32:43 来源: 作者: 【 】 浏览:32
Tags:Linux 下让 bash 脚本 利用 sed 制作 自己 模板

用惯了windows系统的程序员刚开始在Linux文本模式下进行软件开发肯定会觉得不习惯,尤其是没有了集成开发环境,一切都要靠命令行,靠敲键盘,鼠标好像成了摆设!也正因为一切都可以通过命令行来实现,只要肯动脑,多实践,慢慢的你会发现Linux简直就是程序员最合适的操作系统!


C++写程序程序的过程就是实现一个个类的过程,因此写类是再平常不过的工作了。一般会将类声明和实现放在不同的文件中。每个文件都要加上文件头注释,说明该文件的名称、文件内容、作者、时间、版权信息、版本等。为了防止忘记加这些注释,更为了提高工作效率,我想到了做两个模板文件,分别作为声明文件(头文件,后缀为.h)和实现文件(后缀为.cpp)的模板。新建一个类时在此模板基础上稍作修改即可。作为程序员,这个“修改”的过程当然不能手工进行了,要靠“程序”!


先来看看这两个模板文件:


头文件模板如下(文件名:/backup/scripts/h.h):


/**//*
File Name: cyn_class.h


Desc: class cyn_class header file.


Author: Chen Yaning Date: cyn_date


Version: 1.0


Copyright: XXXXX
*/


#ifndef CYN_CLASS_H
#define CYN_CLASS_H


namespace cyn_namespace
...{
//------------------------------class: cyn_class----------------------//
class cyn_class
...{
public:
//constructor
cyn_class(void);


//destructor
~cyn_class(void);


//


private:
//hide copy constructor
cyn_class(const cyn_class &);
cyn_class & operator = (const cyn_class &);


//

};//class cyn_class


}//namespace cyn_namespace


#endif


实现文件模板如下(文件名:/backup/scripts/c.cpp):


/**//*
File Name: cyn_class.cpp


Desc: class cyn_class implement file.


Author: Chen Yaning Date: cyn_date


Version: 1.0


Copyright: XXXXX
*/


#include "cyn_class.h"


namespace cyn_namespace
...{
//-------------------------------class: cyn_class-------------------------//
//constructor
cyn_class::cyn_class(void)
...{
}


//destructor
cyn_class::~cyn_class(void)
...{
}


}//namespace cyn_namespace


模板文件中以cyn_开头和CYN_开头的内容都需要根据实际情况进行“修改”。为了实现自动修改我想到了bash脚本。


对头文件模板进行自动修改的bash脚本如下(文件名:/backup/scripts/newh.sh):


#!/bin/bash


#This script create a new class header file in current dir using the class header template file.
#User can input two params:
#$1 represent the class name you want to create
#$2 represent the namespace you want the class to be in
#if $2 is not spcified DEFAULT_NAMESPACE will be used


#Author: Chen Yaning 2007-11-25


DEFAULT_NAMESPACE=local_converter


TEMPLATE=/backup/scripts/h.h


UPPER=$(echo $1|tr a-z A-Z)


CREATE_DATE=$(date +%Y-%m-%d)


#replace class name and create new file
sed -e "s/cyn_class/$1/g" -e "s/CYN_CLASS/$UPPER/g" $TEMPLATE > $1.h


#set namespace
if [ "$2" != "" ]; then
DEFAULT_NAMESPACE=$2
fi


#replace namespace in the newly created file
sed -i "s/cyn_namespace/$DEFAULT_NAMESPACE/g" $1.h


#replace create date with today
sed -i "s/cyn_date/$CREATE_DATE/g" $1.h
对实现文件模板进行修改的bash脚本如下(文件名:/backup/scripts/newc.sh):


#!/bin/bash


#This script create a new class implement file in current dir using the class implement template file.
#User can input two params:
#$1 represent the class name you want to create
#$2 represent the namespace you want the class to be in
#if $2 is not spcified DEFAULT_NAMESPACE will be used


#Author: Chen Yaning 2007-11-25


DEFAULT_NAMESPACE=local_converter


TEMPLATE=/backup/scripts/c.cpp


UPPER=$(echo $1|tr a-z A-Z)


CREATE_DATE=$(date +%Y-%m-%d)


#replace class name and create new file
sed -e "s/cyn_class/$1/g" -e "s/CYN_CLASS/$UPPER/g" $TEMPLATE > $1.cpp


#set namespace
if [ "$2" != "" ]; then
DEFAULT_NAMESPACE=$2
fi


#replace namespace in the newly created file
sed -i "s/cyn_namespace/$DEFAULT_NAMESPACE/g" $1.cpp


#replace create date with today
sed -i "s/cyn_date/$CREATE_DATE/g" $1.cpp
以上两个bash脚本的功能是根据输入的参数对模板文件中相关的关键字进行替换,同时将当前系统时间也写到文件中(其实就是替换cyn_date),这些“替换”都是通过sed实现的。


将以上两个模板文件和脚本文件保存(我放在了/backup/scripts/目

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux下检查日期是否合法 下一篇LINUX主机运行Python程序注意点

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: