Search code examples
bashtemplatesconfigurationtemplate-engine

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, and path on server.)

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

For example:

generate_new_http_service.sh 8282 subdomain.domain.example /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.

Could I could use python templating engine?


Solution

  • 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.example
    
    This is my template.
    Port is 8080
    Domain is domain.example
    

    or save it to a file:

    $ generate.sh 8080 domain.example > result