One interesting API in Java is to manipulate key-value configuration files known as ‘properties’
I do not know if shell script gurus like to use property files. However, if you are looking for some code to read a key-value pair property file, here is something:
#!/bin/sh
#---------------------
# arg1: prop file
# arg2: prop key
# returns property value if found,
# nothing if not found
#---------------------
get_prop(){
propfile=$1
key=$2
grepĀ "^${2}=" ${1}| sed "s%${2}=\(.*\)%\1%"
}
Suppose you have a properties file such as : test.properties below
user.name=Ranjith init.dir=/tmp
A test call to get value for “user.name” would look like
get_prop test.properties user.name
Advertisement