DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Vue.js Tutorial: Build a Tesla Battery Range Calculator in Vue 3
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • Automated Bug Fixing: From Templates to AI Agents
  • Implement Hibernate Second-Level Cache With NCache

Trending

  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  • Agentic AI for Automated Application Security and Vulnerability Management
  • Cookies Revisited: A Networking Solution for Third-Party Cookies

Bash Script to Generate Config or Property Files From a Template File Containing Variables

Here's a quick way to set up a Bash script when dealing with a template file containing variables.

By 
Venkatt Guhesan user avatar
Venkatt Guhesan
·
Jun. 30, 16 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
18.4K Views

Join the DZone community and get the full member experience.

Join For Free

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”

Property (programming) Bash (Unix shell) Template

Published at DZone with permission of Venkatt Guhesan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Vue.js Tutorial: Build a Tesla Battery Range Calculator in Vue 3
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • Automated Bug Fixing: From Templates to AI Agents
  • Implement Hibernate Second-Level Cache With NCache

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!