I have a file in the format Parmater=value. I want to read the value and pass it to corresponding Variable.
The Parameter file is as follows
Number=23
Text1=mango
Text2=yup
'Number' value needs to be read and passed to ID variable. Also, 'Text1' value needs to be passed to 'Name' Variable and 'Text2' for Department variable.
Following is the code i tried.
while read line
do
c=`awk -F"=" '{print $2}' $line`
case $c in
Number) ID=$line;;
Text1) Name=$line;;
Text2) Department=$line ;;
esac
done < filename.
Hi, you are assigning field 2 to c instead of field 1. Plus, you probably want to assign field 2 to the variables Id, Name and Department instead of the whole line.
I have to read the parameter file whatever value 'Number' is associated to has to be passed to 'ID' Variable. Similarly, Text1 to 'Name' and 'Text2' to 'Department'.
---------- Post updated at 06:16 PM ---------- Previous update was at 06:13 PM ----------
@CarloM, i guess that will help me. The only thing is, i should not make any alterations to the file. Will tr command make any changes to the parameter file?
yeah you are correct. But the variables are not getting assinged. I really appreciate your help but can you point out as what i am doing wrong. Please find the below with the actual file.
Are you running it as an executable, or sourcing the file with '.'?
Executable will create a subshell, so the variables won't be set in the parent, whereas source should run in the current environment. Although, so should running the commands directly...