标签:command function bash linux
############################### # # 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: 咱们出去钓鱼吧!
BASH 文本模版的简单实现 micro_template_compile,布布扣,bubuko.com
BASH 文本模版的简单实现 micro_template_compile
标签:command function bash linux
原文地址:http://blog.csdn.net/wxqee/article/details/31747497