thanks for the tips, i used a "declare" and this worked.
I basically wanted to parse an external config file, and grab whatever parameters are in the config file, and then create bash vars with those parameters, dynamically. Im using an external Py script to parse the actual config file, since Py has built in ConfigParser module, which makes it easy to parse config values.
so for example, heres the conf
cat settings.conf
--------------------------------------------------------------------
[db]
name=xyz
user=joe
[app]
name=abc
user=rita
-------------------------------------------------------------------
cat process.sh // this is main bash script
--------------------------------------------------------------------
config="/etc/someplace/settings.conf"
declare -A params=( [db_name]= [db_user]= [app_name]= [app_user]= )
for param in "${!params[@]}"; do
# get param value from conf file, and create bash var
section=$(echo $param | awk -F"_" {'print $1'})
key=$(echo $param | awk -F"_" {'print $2'})
value=$(ConfigReader.py $config $section $key 2>&1)
params[$param]=$value
# create a bash var dynamically
declare "${section_${key}"=$value
# check if var is empty
if [ -z "${params[$param]}" ];
then
echo "Parameter ${param} is not set, check config file: "$config; echo "exiting.."; exit 1
fi
done
# test var output
echo $db_name
echo $db_user
echo $app_name
echo $app_user
-------------------------------------------------------------------
this is useful when theres a tons load of vars you need to initialize and set and you dont want to stuff them all into a single bash script.
Im working on a better version of this where the Py script reads in all params in the config file, and creates bash vars for everything that it finds, so you dont even need to create an associative array and declare the params manually
heres the Py config parser in case anyone needs it,
cat ConfigReader.py
---------------------------------------------------------------
#!/usr/bin/env python
# This reads a config file parameter and dumps out the value
import ConfigParser
import sys
from sys import argv
import os
conf_file=argv[1]
section=argv[2]
key=argv[3]
config = ConfigParser.ConfigParser()
config.read(conf_file)
value = config.get(section, key)
print value