Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new file from templates with bash script

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...).

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

For example:

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

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.

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

like image 743
kheraud Avatar asked Jun 02 '11 12:06

kheraud


People also ask

How do you create a file in bash script?

To create a new file, run the "cat" command and then use the redirection operator ">" followed by the name of the file. Now you will be prompted to insert data into this newly created file. Type a line and then press "Ctrl+D" to save the file.

What is the format of a bash script?

A basic Bash script has three sections. Bash has no way to delineate sections, but the boundaries between the sections are implicit. All scripts must begin with the shebang (#!), and this must be the first line in any Bash program. The functions section must begin after the shebang and before the body of the program.

How do you create a template file?

Use your template to create a new document To start a new document based on your template, on the File menu, click New from Template, and then select the template you want to use.


1 Answers

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

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 

Output:

$ generate.sh 8080 domain.com  This is my template. Port is 8080 Domain is domain.com 

or save it to a file:

$ generate.sh 8080 domain.com > result 
like image 129
dogbane Avatar answered Sep 22 '22 21:09

dogbane