MyThinkPond

On Java, Python, Groovy, Grails, Spring, Node.js, Linux, Arduino, ARM, Embedded Devices & Web

  • Recent Posts

    June 2016
    M T W T F S S
    « Mar   Jul »
     12345
    6789101112
    13141516171819
    20212223242526
    27282930  
  • Subscribe Options

  • Awards

    JavaCodeGeeks
  • Most Valuable Blogger @ DZone
  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 175 other followers

  • Follow MyThinkPond on WordPress.com
  • Blog Stats

    • 366,804 hits
  • General Options

bash script to generate config or property file from a template file containing variables

Posted by Venkatt Guhesan on June 23, 2016

Sometimes we have configuration or properties file (as templates) such as httpd.conf or server.conf where we want to dynamically replace $variables with values before writing the output to a new file.

Example:

# httpd.conf.tmpl
<Location $STATUS_URI>
    SetHandler server-status
    Order deny,allow
    Deny from all
    Allow from $MONITOR_IP
</Location>

We want to develop an install script in Bash such that it reads the httpd.conf.tmpl and replaces the $STATUS_URI and $MONITOR_IP with appropriate values (either passed in as script arguments or coded in the bash script) and then write out the resulting output to a new file such as /tmp/httpd.conf

Here is such as script:

#!/usr/bin/env bash

# Define the variables with values you want replaced
STATUS_URI="foobar"
MONITOR_IP="192.168.1.1"
# This could also be read in via bash arguments. 
# Google "bash getopts" for more information

# render a template configuration file
# expand variables + preserve formatting
# user="Venkatt"
# referenced inside the template.txt as $user
# render_template /path/to/template.txt > path/to/configuration_file
function render_template() {
  eval "echo \"$(cat $1)\""
}

function generate_httpd_conf {
  echo "#### Creating /tmp/httpd.conf from template ./httpd.conf.tmpl"
  render_template httpd.conf.tmpl > /tmp/httpd.conf
}

Generated output:

<Location foobar>
    SetHandler server-status
    Order deny,allow
    Deny from all
    Allow from 192.168.1.1
</Location>

This can be a useful method to generate .conf .prop (configuration or properties) files.

Cheers.

And now for today’s inspirational quote:

Let us sacrifice our today so that our children can have a better tomorrow.
Abdul Kalam
– 11th President of India from 2002 to 2007
– Chief ‘Rocket’ Scientist born from a poor family with humble beginnings.
– Lived as a “Man of simplicity”

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: