How to Read a Properties File in Linux Shell Scripting

Introduction

In this lesson, we are going to see how to read a properties file properly in Linux Shell Scripting. Let’s give it a go…

Linux shell scripting is a powerful tool for automating tasks and streamlining workflows in a Linux environment. One common task in Linux shell scripting is reading configuration data from a properties file. A properties file is a simple file format that contains key-value pairs, which can be used to store configuration data for a script or application.

We will walk through a step-by-step example of reading data from a properties file and using it in a shell script. By the end of this tutorial, you will have a clear understanding of how to read properties files in Linux and how to use them in your own scripts and applications.

We will have our properties file config.properties and the properties reader shell script file read_properties_file.sh in this example. Save these two files in the same working directory with the following contents;

Method 1: Using “grep”

1. Our properties file config.properties will hold values in a key-value pair:

config.name=Mamun
config.work.hours=39.5
config.location=Ottawa

2. The read_properties_file.sh file has the actual property reader functions:

#!/bin/bash
FILE_NAME="config.properties"
#read the properties file and returns the value based on the key
function getPropVal {
    value= grep "${1}" ./$FILE_NAME|cut -d'=' -f2
	if [[ -z "$value" ]]; then
	   echo "Key not found"
	   exit 1
	fi
	echo $value
}

#Get the value with key
function testPropertyVal {
NAME=($(getPropVal 'config.name'))
echo $NAME
}

#Call the testPropertyVal function
testPropertyVal

Run the script:

$ ./read_properties_file.sh

Method 2: Using the “source” or ".” command

1. Define the properties file: Define the properties file and its location in the shell script. For example, if the properties file is named “config.properties” and is located in the same directory as the shell script.

database.username=CRM_DB
database.host=localhost
database.password="password!"

You can use the following command to define it:

PROPS_FILE=./config.properties

2. Read the properties file: Use the “source” or “.” command to read the properties file and load the properties into variables in the shell script. For example, if the properties file contains the property database.username with the value admin, you can use the following command to load it into a variable in the shell script:

source $PROPS_FILE
USERNAME=$database.username

This command will load the value of database.username from the properties file into the variable USERNAME in the shell script.

3. Use the properties in the shell script: You can now use the properties in the shell script as you would any other variable. For example, you can use the USERNAME variable in a SQL query to connect to the database:

mysql -u $USERNAME -p

This command will use the value of the USERNAME variable to connect to the MySQL database.

Complete example:

PROPS_FILE=./config.properties
source $PROPS_FILE
USERNAME=$database.username

mysql -u $USERNAME -p

Also, see file manipulation and how to Find file in Linux . See complete examples in our GitHub repository.

Follow us on social media

Author