For a very high performance, and BASH 3.0 compatible solution:
file: loadProps.sh
function loadProperties() {
local fileName=$1
local prefixKey=$2
if [ ! -f "${fileName}" ]; then
echo "${fileName} not found!"
return 1
fi
while IFS='=' read -r origKey value; do
local key=${origKey}
key=${key//[!a-zA-Z0-9_]/_}
if [[ "${origKey}" == "#"* ]]; then
local ignoreComments
elif [ -z "${key}" ]; then
local emptyLine
else
if [[ "${prefixKey}${key}" =~ ^[0-9].* ]]; then
key=_${key}
fi
eval ${prefixKey}${key}=\${value}
fi
done < <(grep "" ${fileName})
}
The other solutions provided here are great and elegant, but
- @fork2execve: slow when dealing with large properties files
- @Nicolai: slow when reading lots of properties
- @anubhava: require BASH 4.0 (for the array)
I needed something working on bash 3, dealing with properties files of ~1k entries, reading ~200 properties, and whole script called MANY times.
this function also deals with
- empty lines
- commented code
- duplicated entries (last one wins)
- normalize property names
- last line without a proper new line
Testing
file: my.properties
a=value
a=override value
b=what about `!@#$%^&*()_+[]\?
c=${a} no expansion
d=another = (equal sign)
e= 5 spaces front and back
f=
#g=commented out
#ignore new line below
.@a%^=who named this???
a1=A-ONE
1a=ONE-A
X=lastLine with no new line!
test script
. loadProps.sh
loadProperties my.properties PROP_
echo "a='${PROP_a}'"
echo "b='${PROP_b}'"
echo "c='${PROP_c}'"
echo "d='${PROP_d}'"
echo "e='${PROP_e}'"
echo "f='${PROP_f}'"
echo "g='${PROP_g}'"
echo ".@a%^='${PROP___a__}'"
echo "a1='${PROP_a1}'"
echo "1a='${PROP_1a}'"
echo "X='${PROP_X}'"
loadProperties my.properties
echo "a='${a}'"
echo "1a='${_1a}'"
output
a='override value'
b='what about `!@#$%^&*()_+[]\?'
c='${a} no expansion'
d='another = (equal sign)'
e=' 5 spaces front and back '
f=''
g=''
.@a%^='who named this???'
a1='A-ONE'
1a='ONE-A'
X='lastLine with no new line!'
a='override value'
1a='ONE-A'
Performance Test
. loadProps.sh
function fork2execve() {
while IFS='=' read -r key value; do
key=$(echo $key | tr .-/ _ | tr -cd 'A-Za-z0-9_')
eval ${key}=\${value}
done < "$1"
}
function prop {
grep '^\s*'"$2"'=' "$1" | cut -d'=' -f2-
}
function Nicolai() {
for i in $(seq 1 $2); do
prop0000=$(prop $1 "property_0000")
done
}
function perfCase() {
echo "perfCase $1, $2, $3"
time for i in $(seq 1 1); do
eval $1 $2 $3
done
}
function perf() {
perfCase $1 0001.properties $2
perfCase $1 0010.properties $2
perfCase $1 0100.properties $2
perfCase $1 1000.properties $2
}
perf "loadProperties"
perf "fork2execve"
perf "Nicolai" 1
perf "Nicolai" 10
perf "Nicolai" 100
with 4 NNNN.properties files with entries such as
property_0000=value_0000
property_0001=value_0001
...
property_NNNN=value_NNNN
resulted with
function , file, #, real, user, sys
loadPropert, 0001, , 0.058, 0.002, 0.005
loadPropert, 0010, , 0.032, 0.003, 0.005
loadPropert, 0100, , 0.041, 0.013, 0.006
loadPropert, 1000, , 0.140, 0.106, 0.013
fork2execve, 0001, , 0.053, 0.003, 0.007
fork2execve, 0010, , 0.211, 0.021, 0.051
fork2execve, 0100, , 2.146, 0.214, 0.531
fork2execve, 1000, , 21.375, 2.151, 5.312
Nicolai , 0001, 1, 0.048, 0.003, 0.009
Nicolai , 0010, 1, 0.047, 0.003, 0.009
Nicolai , 0100, 1, 0.044, 0.003, 0.010
Nicolai , 1000, 1, 0.044, 0.004, 0.009
Nicolai , 0001, 10, 0.240, 0.020, 0.056
Nicolai , 0010, 10, 0.263, 0.021, 0.059
Nicolai , 0100, 10, 0.272, 0.023, 0.062
Nicolai , 1000, 10, 0.295, 0.027, 0.059
Nicolai , 0001, 100, 2.218, 0.189, 0.528
Nicolai , 0010, 100, 2.213, 0.193, 0.537
Nicolai , 0100, 100, 2.247, 0.196, 0.543
Nicolai , 1000, 100, 2.323, 0.253, 0.534