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