详细代码

###############################
#
# Funciton: micro_template_compile
#
# Parameter:
# [1] => template :String
# [2..n] => values for placeholder as key=value
#
# Example:
# <- micro_template_compile '{code: {{error.code}}, message: "{{error.message}}"}' "error.code=127" "error.message=command not found."
# ---
# -> { code: 0, message: "" }
###############################
function micro_template_compile() { template_string="$1"; shift;
expression="" while [ $# -ne 0 ]; do
key=$(echo "$1" | sed 's~^\([^=]*\)=\(.*\)$~\1~g')
value=$(echo "$1" | sed 's~^\([^=]*\)=\(.*\)$~\2~g')
expression="s~{{$key}}~$value~g;$expression"
shift;
done echo "$template_string" | sed "$expression"
}

使用案例

运行

micro_template_compile 'Hi {{name}}, it is {{date}} today and {{weather}} outside. :)
message to you: {{message}}' \
  name='小王' date="$(date +"%Y/%m/%d")" weather="大晴天儿" message="咱们出去钓鱼吧。"

输出

Hi 小王, it is 2014/06/16 today and 大晴天儿 outside. :)
message to you: 咱们出去钓鱼吧!

使用到脚本中:

#!/bin/bash

#
# Usage: $exename [options] -in templatefile key=value ...
#
# Examples:
# $exename -in ./1.txt.template -out ./1.txt f1=v1 f2=v2 f3=v3
# $exename -help
# ###############################
#
# Funciton: micro_template_compile
#
# Parameter:
# [1] => template :String
# [2..n] => values for placeholder as key=value
#
# Example:
# <- micro_template_compile '{code: {{error.code}}, message: "{{error.message}}"}' "error.code=127" "error.message=command not found."
# ---
# -> { code: 0, message: "" }
###############################
function micro_template_compile() { local template_string="$1"; shift;
local expression=""
local key=""
local value="" while [ $# -ne 0 ]; do
key=$(echo "$1" | sed 's~^\([^=]*\)=\(.*\)$~\1~g')
value=$(echo "$1" | sed 's~^\([^=]*\)=\(.*\)$~\2~g')
expression="s~{{$key}}~$value~g;$expression"
shift;
done echo "$template_string" | sed "$expression"
} main() {
template="$(cat "$arg_in")"
eval "micro_template_compile '$template' $arg_datagroup > '$arg_out'"
return 0
} processargs() {
# defaults:
arg_in=""
arg_out="stdout"
arg_datagroup="" # arguments:
while echo "$1" | grep "^-" >/dev/null 2>&1; do
case "$1" in
-in)
arg_in="$2"; shift;
;;
-out)
arg_out="$2"; shift;
;;
esac
shift
done while [ $# -ne 0 ]; do
arg_datagroup="$arg_datagroup \"$1\""; shift;
done # exports
export arg_in arg_out arg_datagroup
} processargs "$@"
echo ""
echo "arg_in=|$arg_in|"
echo "arg_out=|$arg_out|"
echo "arg_datagroup=|$arg_datagroup|"
echo "" main

BASH 文本模版的简单实现 micro_template_compile的更多相关文章

  1. MGTemplateEngine 模版引擎简单使用(转)

    原文:http://blog.csdn.net/crazy_srufboy/article/details/21748995 要实现的效果 首先上图中间的 标题至内容 都是使用UIWebView显示, ...

  2. MGTemplateEngine 模版发动机简单使用

    https://github.com/nxtbgthng/MGTemplateEngine MGTemplateEngine 模版引擎 MGTemplateEngine比較象 PHP 中的 Smart ...

  3. Linux Bash文本操作之sed篇其二

    上一篇总结了sed的基础应用(Linux Bash文本操作之sed篇其一),内容实在有够多,这里再对稍微高级一些的用法做一个整理,以方便使用时查阅. 查看文本内容 示例1表示在第一到第四行匹配到的行后 ...

  4. iOS富文本-NSAttributedString简单封装

    直接调用系统的写起来比较麻烦,封装一下 因为要简单所以就写类方法 WJAttributeStyle 基类 ) {         ; i < styles.count; i ++) {      ...

  5. C++函数模版的简单使用

    模版算是C++的独有特性吧,也算是C++中比较难的地方,我平时开发的时候用的非常少,或者几乎没有用到,需要模版的地方是能看懂框架中相关的代码: 模版函数相对还是很简单的,引入模版的目的在于代码的重用: ...

  6. 使用java读取解析txt文本数据,管理简单的数据

    在实际开发中会经常碰到使用编程语言读取文本文件的内容,这内容可以是各种各样的一下本人写出我自己做的一个读取文本文件的例子,文件中存储的是我的个人网站 www.yzcopen.com 导航栏目因为懒得使 ...

  7. Android中的AutoCompleteTextView(随笔提示文本)组件的简单使用

    Android中的随笔提示文本组件AutoCompleteTextView的使用,此组件用于输入文本,然后就会在所配置的适配器中的数据进行查找显示在组件下面. 这里值得注意的是AutoComplete ...

  8. bash帮助文档简单学习;bash手册翻译

    关于bash的四种工作方式的不同,可以参考:http://feihu.me/blog/2014/env-problem-when-ssh-executing-command-on-remote/,但是 ...

  9. bash文本查看及处理工具

    文本查看及处理工具:     wc [OPTION] FILE...         -c: 字节数         -l:行数         -w: 单词数             who | w ...

随机推荐

  1. HNU 12876 Quite Good Numbers 完美数变形

    筛法是一种很快的方法,贴代码纪念一下. 做法很像筛法 #include <iostream> #include <cstdio> #include <cstring> ...

  2. jquery中$.get()提交和$.post()提交有区别

    jquery中$.get()提交和$.post()提交有区别吗? 相同点:都是异步请求的方式来获取服务端的数据: 异同点: 1.请求方式不同:$.get() 方法使用GET方法来进行异步请求的.$.p ...

  3. 【转】 Java 进行 RSA 加解密时不得不考虑到的那些事儿

    [转] Java 进行 RSA 加解密时不得不考虑到的那些事儿 1. 加密的系统不要具备解密的功能,否则 RSA 可能不太合适 公钥加密,私钥解密.加密的系统和解密的系统分开部署,加密的系统不应该同时 ...

  4. Mysql忘记rootpassword

    1,停止MYSQL服务,CMD打开DOS窗体.输入 net stop mysql 2,在CMD命令行窗体,进入MYSQL安装文件夹 比方E:\Program Files\MySQL\MySQL Ser ...

  5. LeetCode_Construct Binary Tree from Inorder and Postorder Traversal

    一.题目 Construct Binary Tree from Inorder and Postorder Traversal My Submissions Given inorder and pos ...

  6. js如何生成[n,m]的随机数(整理总结)

    js如何生成[n,m]的随机数(整理总结) 一.总结 一句话总结: // max - 期望的最大值 // min - 期望的最小值 parseInt(Math.random()*(max-min+1) ...

  7. thinkphp跨模块调用

    thinkphp跨模块调用 跨模块调用模板 return $view->fetch('admin@user/add'); 全路径模板调用: return $view->fetch(APP_ ...

  8. 编写jsp代码时出现的红色提示线错误

    将jsp页面关闭:点击"X"号,例如图中的index.jsp页面的”X"号,或者右键—“close"也可以.   双击jsp页面:重新启动页面,页面的错误提示线 ...

  9. Multi-process Architecture

    For Developers‎ > ‎Design Documents‎ > ‎ Multi-process Architecture This document describes Ch ...

  10. Linux桌面词典 GoldenDict词典

    GoldenDict 是一款不错的.与StarDict(星际译王)类似的词典软件.它使用 WebKit作为渲染核心,格式化.颜色.图像.链接等支持一应俱全:支持多种词典文件格式,包括Babylon的 ...