Shell Script to assign Config values from a file

Post Info: 4,733 views 1 Comments Post a comment

Shell/Bash script to read a config file and assign variables.

#!/usr/bin/sh

# Check the parameters
if [ $# -ne 1 ]
then
  echo -e "Usage: ${0} <Config File>"
  exit 1
else
  CONF_FILE=$1
fi

echo -e "Starting script $0\n";

# Read config file and assign values

for i in `cat $CONF_FILE | grep '^[^#].*'`
do
   var=`echo "$i" | awk -F"=" '{print $1}'`
   param=`echo "$i" | awk -F"=" '{print $2}'`
   eval $var=$param
done

# Print a sample valeue
echo $DB_Name

echo -e "Script $0 Finished\n";

Here is the sample configuration file.

# Config Variables

# Directory and File
Process_Dir=/var/process/
Data_Dir=/mnt/

# DB User
DB_Host=localhost
DB_Name=test
DB_User=test

#EmailIds
Email_Ids=someone\@example.com

Return to top

1 Comment

  • Sri says on January 13, 2013 at 3:53 pm | Permalink

    It would have been more clear for people like mw who are not experts in Unix. Can you please go though the for loop and explain what you are doing in condition and the statements inside for loop.

    The articles are great in this blog.
    Thanks.

Leave a Reply

 

Are you finished?

Return to top