Thursday, August 13, 2009

shell read properties file



Problem :

You want to read a properties files that is similar to :

vi test.properties

VAR1=zest
VAR2=blop
VAR3=toto
MAIL=test@ty
ALIAS=monalias

You want also use this variable.


Solution

You have to read line by line and make an export of each line.
Note : It's just a test method.


#!/bin/ksh
#-----------------------------
------------------------------
# Methode qui lit les proprietes d un fichier de properties.
#-----------------------------------------------------------

readFileProperties()
{
FILE="test.properties"

test -f $FILE
if [ $? -ne 0 ];then
echo "Fichier de propriete inexistant:$FILE"
exit 1
fi

echo "Lecture..."
while read ligne
do
if echo "$ligne" |grep "=" 1>/dev/null
then
VARNAME=`echo $ligne |cut -f1 -d=`
#echo $VARNAME
VARVALUE=`grep "^$VARNAME=" $FILE`
export $VARVALUE
#echo "export $VARVALUE"
fi
done < $FILE echo "VAR1:$VAR1" echo "VAR2:$VAR2" echo "VAR3:$VAR3" echo "ALIAS:$ALIAS" echo "MAIL:$MAIL" } readFileProperties

No comments:

Post a Comment