使用bash脚本从模板创建新文件 [英] Create new file from templates with bash script

查看:119
本文介绍了使用bash脚本从模板创建新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建非常相似的conf文件和init.d.这些文件允许在我的服务器上部署新的http服务.这些文件是相同的,只有一些参数从一个文件更改为另一个文件(listen_port,域,服务器上的路径...).

I have to create conf files and init.d which are very similar. These files permit to deploy new http service on my servers. These files are the same and only some parameters change from one file to another (listen_port, domain, path on server...).

由于这些文件中的任何错误都会导致服务故障,因此我想使用bash脚本创建这些文件.

As any error in these files leads to misfunction of service I would like to create these files using a bash script.

例如:

generate_new_http_service.sh 8282 subdomain.domain.com /home/myapp/rootOfHTTPService

我正在寻找一种可以与bash一起使用的模板模块.该模板模块将使用一些通用的conf和init.d脚本来创建新的脚本.

I am looking for a kind of templating module that I could use with bash. This templating module would use some generic conf and init.d scripts to create new ones.

您有提示吗?如果没有,我可以使用python模板引擎.

Do you have hints for that? If not I could use python templating engine.

推荐答案

您可以使用Heredoc进行此操作.例如

You can do this using a heredoc. e.g.

generate.sh:

generate.sh:

#!/bin/sh

#define parameters which are passed in.
PORT=$1
DOMAIN=$2

#define the template.
cat  << EOF
This is my template.
Port is $PORT
Domain is $DOMAIN
EOF

输出:

$ generate.sh 8080 domain.com

This is my template.
Port is 8080
Domain is domain.com

或将其保存到文件中

$ generate.sh 8080 domain.com > result

这篇关于使用bash脚本从模板创建新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆