In this Shell Script section, Let us see how to fetch the Value from the Property File based on the Key.
Consider a Property File named "Test.prop" which is of the below format,
Test.prop
Age=24
Occupation=Farmer
In the above Property file, Let us fetch the Value for the key (i.e., Name) and display the same. Go through the below script file once so that we can discuss the steps involved.
ReadProperty.sh
FILE_NAME=Test.prop
# Key in Property File
key="Name"
# Variable to hold the Property Value
prop_value=""
getProperty()
{
prop_key=$1
prop_value=`cat ${FILE_NAME} | grep ${prop_key} | cut -d'=' -f2`
}
getProperty ${key}
echo "Key = ${key} ; Value = " ${prop_value}
In the above script, the method getProperty() method identifies the Property file to be read and fetches the entire row using the Key (i.e., Name). Now it will have the complete string i.e., Name=Admin.
Our motto is to fetch only the Value i.e., Admin. Hence using the cut command, the Value is separated using the delimiter ("=").
Though its a simple one, hope this will be useful for beginners!