BASH 文本模版的简单实现 micro_template_compile
来源:互联网 发布:剑三正太捏脸数据截图 编辑:程序博客网 时间:2024/06/06 15:00
具体代码
################################# 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
0 0
- BASH 文本模版的简单实现 micro_template_compile
- 一个简单的文本查询程序------bash sample(2)
- 实现简单的文本加密
- c++模版及模版类简单实现
- 模版的简单使用
- 简单的函数模版
- 简单的继承模版
- 简单模版的使用
- 由Java实现简单的文本编辑器
- 文本掩码的一种简单实现
- 文本数据库的简单java实现
- Javascript实现简单的富文本编辑器
- python图片文本识别的简单实现
- Storyboard实现简单的文本自适应
- python图片文本识别的简单实现
- js实现一些简单文本的加载
- python 实现简单的文本聚类
- bash实现一个简单的错误(mutation)注入工具
- IAR for AVR_Proteus—联合调制
- 简单配置struts2
- java单元测试技术
- Reorg
- Spring注释@Autowired @Resource @Qualifier @Required @Resource @Component,@Service,@Controller,@Reposit
- BASH 文本模版的简单实现 micro_template_compile
- 初学maven(2)-maven/artifactory/m2eclipse安装全过程
- 30天自制操作系统(第一天)
- poj3420
- 初学maven(3)-使用nexus替代artifactory作为maven私服
- 黑马程序员_集合
- Linux查找多个类似但是不同名的文件并且重命名
- 迭代器简述(二)
- BIND或REBIND