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/目录下),然后在文件 ~/.bashrc中添加两个别名分别指向newh.sh和newc.sh:

alias newh='bash /backup/scripts/newh.sh'
alias newc='bash /backup/scripts/newc.sh'

这样在任何目录下只要输入“newh  类名 命名空间名”就会在该目录下生成相应的头文件,输入“newc  类名  命名空间名”就会生成实现文件。

相关内容