In this tutorial, You will Learn

  • How to read or parse properties files in shell script
  • Write to a Properties file in Shell Programming

this post covers how to Read the key and its values from a properties file and display it to the console

Let’s declare the properties file

database=student
hostname=localhost
username=root
password=root

The properties file contains multiple lines where each line contains a key and value separated by equal(=).

Shell Scripts provides associative arrays to store an array of key and value pairs.

You can declare and initialize associative arrays using the below lines of code

declare -A properties

How to read properties file in Shell Script with examples?

There are multiple ways to read properties files in Shell Script

Here is a step-by-step tutorial

  • Create a properties array using the associated array syntax
  • Read Properties using a while loop,
  • while loop contains condition
    • read each line from a file using read -r, iterate until all lines are parsed
    • separate the line into key and value using IFS, Input Field Separator
    • Ignore comments that start with a line #, other lines are parsed and saved to an array with key and value pairs
  • Finally, Iterate the array using for loop
for key in "${!properties[@]}"; do
    echo "$key = ${properties[$key]}"
done

Here is an example

#!/bin/bash

file="config.properties"

# read_properties function to parse key and value of a properties file
read_properties() {
    while IFS='=' read -r key value; do
        # Ignore lines starting with # (comments) and empty lines
        if [[ ! $key =~ ^\s*# && -n $key ]]; then
            properties["$key"]=$value
        fi
    done < "$file"
}

# Initialize associative array to store properties
declare -A properties

# Read properties from the file
read_properties

# Display properties to console
echo "Properties file content:"
for key in "${!properties[@]}"; do
    echo "$key = ${properties[$key]}"
done

Output:

Properties file content:
database = student
hostname = localhost
username = root
password = root

How to read properties file using grep

This example reads properties using grep and splits the key and value using the cut -d command.

#!/bin/bash

file="./config.properties"

function readproperties {
    grep "${1}" ${file} | cut -d'=' -f2
}

echo "username = " $(readproperties 'username')
echo "password = " $(readproperties 'password')
echo "hostname = " $(readproperties 'hostname')
echo "database = " $(readproperties 'database')

How to write a key and values to a properties file in Shell

In this example, You Shell Associated arrays written to properties files.

  • First, Create an empty file
  • create an Associative Array, initialize with empty values
  • Add the key and values to an array
  • Iterate array and write to a file using the redirection operator >>, It appends the key and value to a file.

Here is an complete example of reading and write a properties file

#!/bin/bash

file="config1.properties"

# Create a file using ls >
touch $file

# Function to write properties to file
write_properties() {
    for key in "${!properties[@]}"; do
        echo "$key=${properties[$key]}" >> "$file"
    done
}

# Initialize associative array to store properties
declare -A properties



# Update or add new properties
properties["host"]="localhost"
properties["username"]="root"
properties["database"]="student"
properties["password"]="root"

# Write properties to file
write_properties

Output of a file

database=student
username=root
password=root
host=localhost

The above code does not check

  • permission to create a file
  • It appends the content to a file if exists.
  • Not checked file exists or not

Conclusion

You learned to read a properties file with keys and values as well as line-by-line and also write keys and values to the properties file