retrive value from a file

hi
i am new to shell scripting

i have a properties file like
hs=abc
hs1=def
hs2=ghi

now i want to retrive each value and assign it to a variable
like var1 = abc

please help

If the file's syntax is compatible with the shell's, you can simply use the source command, the lone dot:

. file.cf

With the example you have provided, this would assign "abc" to the variable hs, "def" to hs1, and "ghi" to hs2.

If that's not what you want, you could perhaps read in one line at a time, remove everything up through the first equals sign, and then assign the result to some variable.

for var in var1 var2 var3
do
    read line
    eval $var=\""${line#*=}"\"
done <file.cf

This reads three lines and assigns anything after the first equals sign on each of those lines to the selected variable name; successively, var1, var2, and var3.

The use of eval is a rather advanced technique; I would advise against this if you can come up with a simpler solution.

You mean this ?

for val in $(cut -f 2 -d'=' buf); do var1=$val; echo $var1; done

hi era
i am able to retrieve a value from a file, but i dont know how to assign the retrieved value to a variable. the code i have is
#regressiontest.cfg
hs=abc
hs1=def
hs2=ghi
now i am retrieving the value from the above file
RetrieveCfgvalue()
{
CFG_VALUE=`grep "$2=" $CONFIG_FILE_NAME | cut -d"=" -f2`
CFG_VALUE=`echo $CFG_VALUE|sed -s "s/ *//g"`
CFG_VALUE=`echo $CFG_VALUE|sed -s "s/\"//g"`
if [ ! -z $CFG_VALUE ]
then
eval "$1=$CFG_VALUE"
fi
}

CONFIG_FILE_NAME="regressiontest.cfg"
RetrieveCfgvalue hs hs

now i want to assign the retrieved value(hs) to a variable like
var1 = abc
please help quickly

Your function looks like it already does this. After RetrieveCfgvalue hs hs you should have the value of hs in the variable hs, isn't that correct? Then if you also want to assign it to var1, you can say var1=$hs (or simply RetrieveCfgvalue var1 hs directly, if that was what you were planning anyway).

hi era
the proble occuring is i have
hs=lpdma520.dev.ipc.us.aexp.com
hs1=lpdma521.dev.ipc.us.aexp.com
hs2=lpdma522.dev.ipc.us.aexp.com
like this in the cfg file.
When i tried to retrieve the value by using the above function
i am getting "command not found"
can u please help

Based on the additional information in the duplicate thread http://www.unix.com/shell-programming-scripting/65542-retrieve-value-file.html\#post302196206 I would say the problem is that you can't have full stops in variable names.

Please don't post duplicate threads.

Just to clarify things. You can have periods (full stops) in variable names but only in shells that support hierarchical name spaces (AKA compound variables) such as ksh93.

#!/usr/bin/ksh93

myvar=( name="era" job="herder" rest="of Useless Cats" )
var="era"
var.name="herder"
var.name.rest="of Useless Cats"

print ${myvar.rest}
print ${var.name.rest}