};//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-25DEFAULT_NAMESPACE=local_converterTEMPLATE=/backup/scripts/h.hUPPER=$(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-25DEFAULT_NAMESPACE=local_converterTEMPLATE=/backup/scripts/c.cppUPPER=$(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/目录下),然后在文件 ~/.bashrc中添加两个别名分别指向newh.sh和newc.sh:alias newh="bash /backup/scripts/newh.sh" alias newc="bash /backup/scripts/newc.sh"这样在任何目录下只要输入“newh 类名 命名空间名”就会在该目录下生成相应的头文件,输入“newc 类名 命名空间名”就会生成实现文件。