DEV Community

Cover image for Bash script that automating the process of updating a configuration file based on environment variables
Muzaffar khan
Muzaffar khan

Posted on

Bash script that automating the process of updating a configuration file based on environment variables

Certainly! Let's write a straightforward Bash script to show how changing a configuration file based on environment variables can be automated.

For the purposes of this illustration, let's imagine you have a configuration file called app_config.conf that needs to be changed with various settings based on the environment (development, testing, production).

Assume you have a configuration file named app_config.conf with the following content:

  • app_config.conf
API_URL=
DB_HOST=
Enter fullscreen mode Exit fullscreen mode
#!/bin/bash
config_file="app_config.conf"

if [ $# -ne 3 ]; then
    echo "Usage: $0 <environment> <api_url> <db_host>"
    exit 1
fi

ENVIRONMENT="$1"
api_url="$2"
db_host="$3"

echo "API_URL=$api_url" > "$config_file"
echo "DB_HOST=$db_host" >> "$config_file"

echo "Configuration updated for $ENVIRONMENT environment."

Enter fullscreen mode Exit fullscreen mode

How Do I Use This Script?

  1. Make it executable:
chmod +x update_config.sh
Enter fullscreen mode Exit fullscreen mode
  1. Run the script:
./update_config.sh development http://dev.api.example.com dev-db.example.com
Enter fullscreen mode Exit fullscreen mode

Top comments (0)