Pass value from file to parameter

Hi Guys,

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.

Can somebody guide me where i am doing wrong.

Cheers!!!!!

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.

Are 'a', 'b', 'c' the actual values in your input file, or are they 'ID', 'Name', 'Department'?

If they're arbitrary characters, you could do:

tr '=' ' ' < filename| while read par val
do
   case $par in
      'a') ID=$val;;
      'b') NAME=$val;;
      'c') DEPARTMENT=$val;;
   esac
done

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?

No - it just writes to stdout (which is being piped to the while).

If your actual input file is more complex you can still use awk, just pipe the output of that into the while instead.

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.

mac> cat Dallysales.prm
GP_BATCH_TRK_ID=56
GP_IP_FILE1=/appl/data/Input/US/DAILYSLS_US_LOAD*.txt
GP_SLS_DT_CURR_YR=20110524
GP_COUNTRY=US
GP_BUSINSS_UNIT=GMUS
mac> tr '=' ' ' < Dallysales.prm | while read parm value
> do
> case $parm in
> 'GP_BATCH_TRK_ID') Tracking_ID=${value} ;;
> 'GP_IP_FILE1') File=${value} ;;
> 'GP_SLS_DT_CURR_YR') Sales_date=${value} ;;
> 'GP_COUNTRY') Country=${value} ;;
> 'GP_BUSINSS_UNIT') BU=${value};;
> esac
> done
mac> echo $File
mac> echo $Sales_date
mac> echo $BU
mac>

That should work (at least it does on the ksh I'm using), but it may be that your shell is executing the commands in a subshell.

Try putting the script in a file and doing . ./scriptname.sh .

Something weird is happening. It is getting assigned but when called for, i am not able to retrive the value. Check the logs below.

#!/usr/bin/ksh
tr '=' ' ' < Dallysales.prm | while read parm value
 do
 case $parm in
 'GP_BATCH_TRK_ID') Tracking_ID=${value} ;;
 'GP_IP_FILE1') File=${value} ;;
 'GP_SLS_DT_CURR_YR') Sales_date=${value} ;;
 'GP_COUNTRY') Country=${value} ;;
 'GP_BUSINSS_UNIT') BU=${value};;
 esac
 done
+ tr = ' '
+ read parm value
+ case $parm in
+ Tracking_ID=56
+ read parm value
+ case $parm in
+ File='/appl/data/Input/US/DAILYSLS_US_LOAD*.txt'
+ read parm value
+ case $parm in
+ Sales_date=20110524
+ read parm value
+ case $parm in
+ Country=US
+ read parm value
+ case $parm in
+ BU=GMUS
+ read parm value
echo $Tracking_ID;
+ echo
echo $File;
+ echo
echo $Sales_date;
+ echo
echo $Country;
+ echo
echo $BU;
+ echo

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...

I am running it dirrectly from the shell script. There is no subshell. There is only shell with these commands thats all.

I even gave export variablename=value. but still it is not working. Can you please let me know how to capture it in variables.

cheers!!!

---------- Post updated 12-06-11 at 11:27 AM ---------- Previous update was 12-05-11 at 07:34 PM ----------

Hi, Can somebody help me to understand why even after the assignment the values are not geting reflected on recall (echo)..

Cheers!!!!