4

What is the best way to source a config file in bash and import their variables/contents in a script? Let's say, I have a config file called /path/index.conf, which is like:

VAR1=
VAR2 = 1
VAR3=A&C
#comment
PASSWORD=xxxx
EMAIL=abc@abc.com

I am writing a shell script that will do the following:

1) If the config file does not exist, then the script will create the config file with default values.

2) If any of the variable's value is missing, the script will replace those specific variables with default values. For example VAR1= value is missing and it should be replaced with VAR1=0

3) The script should skip comments and whitespaces (for example, VAR2 = 1 some whitespaces), and complain if any of the lines contains $,%, and & character

This what I have done so far:

#!/bin/sh

#Check the config file
source_variables () {
    source /path/index.conf
    #TODO: if the file does not exist, create the file
    #TODO: file exists, but missing some variables, fill them with default values
    test -e /path/index.conf
    if test "$VAR1" = "0" ; then
        echo "VAR1 successfully replaced "
    fi

   #TODO: If any variable contains $,%, and & (for example VAR3). Flag it and return 1.

   #import all the variables and declare them as local
   local var_a = $VAR1
   local var_b = $VAR2
   # ...etc  
   return 0 #checking successful  
}

I am learning bash scripting, and I understand that my approach is incomplete and might not be the best practice out there. Can anyone help me?

Sedmaister
  • 475
  • 1
  • 4
  • 11

1 Answers1

3

You can't use source here because your example config is not valid bash code. Sourcing a file means including it as it is - it's like #include in C world. Instead, use a proper INI parser such as https://github.com/rudimeier/bash_ini_parser. Your script could look like this:

#!/usr/bin/env sh

config=config

var1_default=0
var2_default=0
var3_default=0
password_default="default_password"
email_default="default@email.com"

source_variables () {
    if [ ! -f "$config" ]
    then
    printf "No config found, creating default config\n" >&2
    {
        echo VAR1="$var1_default" >> "$config"
        echo VAR2="$var2_default" >> "$config"
        echo VAR3="$var3_default" >> "$config"
        echo PASSWORD="$password_default" >> "$config"
        echo EMAL="$email_default" >> "$config"
    } >> "$config"
    fi

    . "$PWD"/read_ini.sh
    read_ini "$config"

    var1="${INI__VAR1:-${var1_default}}"
    var2="${INI__VAR2:-${var2_default}}"
    var3="${INI__VAR3:-${var3_default}}"
    password="${INI__PASSWORD:-${password_default}}"
    email="${INI__email:-${email_default}}"
}

source_variables

echo VAR1: "$var1"
echo VAR2: "$var2"
echo VAR3: "$var3"
echo PASSWORD: "$password"
echo EMAIL: "$email"
Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
  • 1
    See the "Answer Well-Asked Questions" section in [How to Answer](https://stackoverflow.com/help/how-to-answer), particularly the bullet point regarding questions which "*have already been asked and answered many times before*". – Charles Duffy Jul 30 '18 at 19:36