Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reading the properties file using shell script

25 views
Skip to first unread message

bubunia...@gmail.com

unread,
Mar 24, 2018, 12:54:12 PM3/24/18
to
Hi all,

I have a property file which has both list as well as key=value pair.

For example :

a.properties

A1=X1
B1=[Y1,Z1,W1]

I want to read each key value arguments and set it in my script.
For example test.sh to create a separate dynamic flat file based on the parsed key pair values.

Can I read the values as list for a key using shell script?

If so can anyone provide me an example for the same?

I tried it but was unsuccessful and it gives error: return: "X1": numeric argument required. This is due to to the fact that shell script can only accept numeric return code not string or list etc. How can I tackle this problem and any cool idea?


FILE_NAME="./a.properties"

function getProperty()
{
prop_key=$1
prop_value=`cat ${FILE_NAME} | grep ${prop_key} | cut -d'=' -f2`
return $prop_value

}

function readPropFile() {

local propFileName=$1

# Key in Property File
key="Name"

# Variable to hold the Property Value
prop_value=""


testA1=$(getProperty "A1")

echo "A:$testA1"

}



readPropFile $FILE_NAME

Regards
Pradeep

Lew Pitcher

unread,
Mar 24, 2018, 3:10:28 PM3/24/18
to
bubunia...@gmail.com wrote:

> Hi all,
>
> I have a property file which has both list as well as key=value pair.
>
> For example :
>
> a.properties
>
> A1=X1
> B1=[Y1,Z1,W1]
>
> I want to read each key value arguments and set it in my script.
> For example test.sh to create a separate dynamic flat file based on the
> parsed key pair values.
>
> Can I read the values as list for a key using shell script?

Yes.

>
> If so can anyone provide me an example for the same?

15:07 $ cat ./a.properties
A1=X1
B1=[Y1,Z1,W1]

15:07 $ ./getproperty.sh A1
For KEY A1, VALUE is X1

15:07 $ ./getproperty.sh B1
For KEY B1, VALUE is [Y1,Z1,W1]

15:07 $ ./getproperty.sh C1
No VALUE for KEY C1

15:07 $ cat ./getproperty.sh
#!/bin/bash

function GetProperty()
{
cat "$1" |
while read LINE ;
do
IFS='=' KVPAIR=( ${LINE} )
if [ "$2" = "${KVPAIR[0]}" ]
then
echo "${KVPAIR[1]}"
fi
done
}

KEY=$1

VALUE=$(GetProperty "./a.properties" "${KEY}")

if [ -n "${VALUE}" ]
then
echo For KEY "${KEY}", VALUE is "${VALUE}"
else
echo No VALUE for KEY "${KEY}"
fi



HTH
--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

bubunia...@gmail.com

unread,
Mar 26, 2018, 1:16:31 AM3/26/18
to
Thanks for the code. But I guess this will make the properties file will be read multiple times which may decrease performance. Can we get the properties content in an associate array without reading multiple times?

Lew Pitcher

unread,
Mar 26, 2018, 10:27:45 AM3/26/18
to
bubunia...@gmail.com wrote:
[snip]
> Thanks for the code. But I guess this will make the properties file will
> be read multiple times which may decrease performance. Can we get the
> properties content in an associate array without reading multiple times?

Sure. You could build that from the example code I posted, but the simplest
way would be to "source" the properties file, then use the property key as a
shell variable

For example:

10:25 $ cat ./a.properties
A1=X1
B1=[Y1,Z1,W1]

10:25 $ echo $A1

10:25 $ echo $B1

10:25 $ echo $V1

10:25 $ source ./a.properties

10:26 $ echo $A1
X1

10:26 $ echo $B1
[Y1,Z1,W1]

10:26 $ echo $V1

10:26 $
0 new messages