如何在bash中填充文档模板

时间:2016-01-14 04:09:28

标签: bash shell

我必须使用不同命令的输出填充(降价)文档模板。为了简单起见,假设你有一个这样的模板(我的模板要复杂得多):

$ cat sample_template.txt
Memory availability:
--------------------
<insert free output here>

Storage availability:
---------------------
<insert df output here>

如何在正确的位置插入每个命令输出?

1 个答案:

答案 0 :(得分:2)

使用m4

$ cat sample_template.txt
Memory availability:
--------------------
INSERT_FREE_OUTPUT_HERE

Storage availability:
---------------------
INSERT_DF_OUTPUT_HERE

$ m4 -DINSERT_FREE_OUTPUT_HERE="$(free)" -DINSERT_DF_OUTPUT_HERE="$(df -h)" < sample_template.txt

Memory availability:
--------------------
             total       used       free     shared    buffers     cached
Mem:                  <my memory details get printed here>
-/+ buffers/cache:    <my memory details get printed here>
Swap:                 <my memory details get printed here>

Storage availability:
---------------------
Filesystem      Size  Used Avail Use% Mounted on
<my filesystem details get printed here>