Shell script to read java-style property files

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.