Reputation: 349
How to read config.properties
file using shell script?
Example file:
key1 = This is 1st value
key2 = this is 2nd value
key3
Upvotes: 4
Views: 16139
Reputation: 179
Here is a simplified version of the (bash) script I use in my projects:
# reading CONFIG FILE
# usage: readConf <fileName> <several variable names>
function readConf {
local confFile=$1
shift
local allowedVars="$@" # only specified variable names will be processed
local forbidden=" #@%*()<>~'\"{}[]\$"
for VN in $allowedVars; do
unset $VN
done
while IFS== read -r key val ; do
var=${key//["$forbidden"]}
if [[ " ${allowedVars[@]} " =~ " $var " ]]; then
val=${val//["$forbidden"]}
eval "${var}=\"${val}\""
else
if ! [ -z $var ]; then
echo "conf parameter: \"$var\" is not supposed to be defined in \"$confFile\""
fi
fi
done < <(grep -v "^#" $confFile)
}
It scans config file (first argument) for specified parameters (remaining arguments). Certain "forbidden symbols" are ignored to avoid code injections. Lines starting with '#' are ignored.
Upvotes: 0
Reputation: 1175
You can try this. its also contain database operations:you can get all answers that you wanted if you clearly understood this code
#!/bin/bash
PROPERTY_FILE=filename.properties
function getProperty {
PROP_KEY=$1
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
echo $PROP_VALUE
}
echo "# Reading property from $PROPERTY_FILE"
DB_USER=$(getProperty "db.username")
DB_PASS=$(getProperty "db.password")
ROOT_LOC=$(getProperty "root.location")
echo $DB_USER
echo $DB_PASS
echo $ROOT_LOC
echo "Writing on DB ... "
mysql -u$DB_USER -p$DB_PASS dbname<<EOFMYSQL
update tablename set tablename.value_ = "$ROOT_LOC" where tablename.name_="Root directory location";
EOFMYSQL
echo "Writing root location($ROOT_LOC) is done ... "
counter=`mysql -u${DB_USER} -p${DB_PASS} dbname -e "select count(*) from tablename where tablename.name_='Root directory location' and tablename.value_ = '$ROOT_LOC';" | grep -v "count"`;
if [ "$counter" = "1" ]
then
echo "ROOT location updated"
fi
Upvotes: 2
Reputation: 1173
Try the following:
#!/bin/bash
# Read configuration into an associative array
declare -A CONFIG
# IFS is the 'internal field separator'. In this case, your file uses '='
IFS="="
while read -r key value
do
if [ -n $value ]; then
CONFIG[$key]=$value
else
CONFIG[$key]=$value
fi
done < YOUR_CONFIG_FILENAME
unset IFS
# If a parameter is passed, look it up by that, else print everything.
if [ $1 ]; then
if [ -n ${CONFIG[$1]} ]; then
echo "Key: $1, Value: ${CONFIG[$1]}"
else
echo "The key '$1' does not exist"
fi
else
for key in "${!CONFIG[@]}"; do
if [ -n ${CONFIG[$key]} ]; then
echo "Key: $key, Value: ${CONFIG[$key]}"
else
echo "Key: $key has no value"
fi
done
fi
exit $?
It will read in all keynames in the config file and inform you if no value is set against the key, meeting (1) and (2) of your requirements.
I don't quite understand what the requirements are for (3) though
"3) enter any key that key related value display" Update the script with that requirement.
Upvotes: 4