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
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Mainframe Development for the "No Mainframe" Generation
  • When Scrum Feels Like Dressing for Dinner
  • 3 Examples of Address Autofill Forms to Collect Shipping and Payment Addresses
  • The Differences Between Bash, Source, ".", and "./" Execution

Trending

  • Spring Boot and React in Harmony
  • The Stairway to Apache Kafka® Tiered Storage
  • Automated Testing Lifecycle
  • Wild West to the Agile Manifesto [Video]

Bash Snippet: Reading Values From a Configuration File

In this article, we tackle the problem of simplifying bash commands. Sound intriguing? Read to learn the answer this developer came up with!

Bipin Patwardhan user avatar by
Bipin Patwardhan
·
Oct. 03, 17 · Tutorial
Like (1)
Save
Tweet
Share
8.24K Views

Join the DZone community and get the full member experience.

Join For Free

Recently, I was developing a bash script. To make the script flexible and configurable, we decided to maintain some of the values required by the application, in a configuration file. If a change was needed, we only needed to edit the config file and not the script, making script maintenance easier.

To get values from the config file, I used 'grep' and 'cut', as follows:

 schema_name=`grep "schema_name" config.cfg | cut -d '=' -f 2`
 database_url=`grep "database_name" config.cfg | cut -d '=' -f 2`

While the contents of the config file were:

schema_name=test

database_url=database.mydomain.com 

If we need to fetch a lot of values from the config file, this method becomes repetitive and can be error-prone. Hence, I encapsulated the grep and cut commands inside a function, making it easier to fetch values, as follows:

getValueFromConfig() {
    VALUE=`grep ${1} config.sys | cut -d '=' -f 2`
}

In the defined function, ${1} is the first parameter passed to the function and is used to pass the key, whose value is to be read from the config file.

The way to use this function is

schema_name=`getValueFromConfig "schema_name"` 

or

schema_name=$(getValueFromConfig "schema_name") 

In fact, it is also possible to further customize the function to parameterize the separator used in the config file, as well as the position of the value that needs to be fetched from the config file.

Bash (Unix shell) Snippet (programming)

Opinions expressed by DZone contributors are their own.

Related

  • Mainframe Development for the "No Mainframe" Generation
  • When Scrum Feels Like Dressing for Dinner
  • 3 Examples of Address Autofill Forms to Collect Shipping and Payment Addresses
  • The Differences Between Bash, Source, ".", and "./" Execution

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • 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: