How to get the value of a variable which is having another value in environmental script?

Dear Folks,
I am facing an issue in getting a value of a variable.Let me explain the scenario.

I am having a list file say files.list in which I have mentioned

1  FILE1
          2  FILE2

Then I am having an set_env.ksh in which I mentioned

FILE1=/clocal/data/user/userdata.txt
FILE2=/clocal/data/scripts/script.ksh
export FILE1 FILE2

Then I am having a main script in which I want to accesses the value of FILE1 by giving "files.list" as input. I am doing something like this

#!/usr/bin/ksh
.set_ki_env.ksh
 
##Function##
getFileName(){
FileName=$1;
echo $FileName
##This is printing the value from list file which is FILE1
}
 
##End###
 
 
while read num filename
do
getFileName $filename
done < files.list

I am getting the output as FILE1. I want to get the value of FILE1 from the set_env.ksh which is /clocal/data/user/userdata.txt.

I have tried $("$FileName"); But not getting the required output.

Please help me with this.
Thanks in advance

You have to "source" the file with the variables like:

. ./set_ki_env.ksh

To get the value of the variable you can use the eval command like:

#!/usr/bin/ksh

. ./set_ki_env.ksh

while read num filename
do
  eval "file=\$$filename"
  echo $file
  ...
  ...
done < files.list
eval "file=\$$filename"

This is the absolute answer for my question. Thanks a lot..It is working:b:

What does the \$$filename do? I don't think I have ever seen this before.

eval is used here to obtain the value of a variable whose name is derived from the value of another variable.

Suppose you have an environment variable FILE1 with the content as in the scenario above:

FILE1="/clocal/data/user/userdata.txt"

If you assign the name of the environment variable to the variable like:

filename="FILE1"

filename doesn't have the value "/clocal/data/user/userdata.txt" but just "FILE1".

With the command:

eval "file=\$$filename"

eval expands $filename to FILE1, then the shell expands $FILE1 (hence the escaped $) and assigns the content to the variable $file.

Now the variable $file contains "/clocal/data/user/userdata.txt".

1 Like