To print value for a $variable inside a $variable or file

Hi guys,

I have a file "abc.dat" in below format:

FILE_PATH||||$F_PATH
TABLE_LIST||||a|b|c
SYST_NM||||${SRC_SYST}

Now I am trying to read the above file and want to print the value for above dollar variables F_PATH and SRC_SYST. The problem is it's reading the dollar variables as literals and not giving the values for those variables. Below is the script I am using:

echo `echo $(cat abc.dat|head -1|awk -F'|'+ '{print $2}')`

The above command gives me $F_PATH . But now how to get the value for this parameter, I am unable to do and kinda stuck.

Appreciate your help in advance.

Try:

eval echo `echo $(cat abc.dat|head -1|awk -F'|'+ '{print $2}')`

OSX 10.7.5, default shell.
I am assuming you want just some values so therefore I created some to test with.
A version using __builtins__ only.
(Note:- your code on this machine causes odd errors so......)

#!/bin/sh
# Create a file as per the original.
echo "FILE_PATH||||\$F_PATH
TABLE_LIST||||a|b|c
SYST_NM||||\${SRC_SYST}" > /tmp/text
# Use "hexdump" as a proof test to check that it is as the original.
hexdump -C < /tmp/text
# Set up the terminal ready.
ifs_str="$IFS"
# Thanks Corona688 for this little snippet.
IFS="
|"
n=0
# Just simulate some path...
F_PATH="/tmp"
# Assume below is a number.
SRC_SYST="3"
# Extracing the variables...
read -d '' line < /tmp/text
echo "$line"
# This is the working code using __builtins__ only...
line_array=($line)
for n in $( seq 0 1 ${#line_array[@]} )
do
	if [ "${line_array[n]}" == '$F_PATH' ]
	then
		eval line=$(echo "${line_array[n]}")
		# This should be "/tmp"...
		echo "$line"
	fi
	if [ "${line_array[n]}" == '${SRC_SYST}' ]
	then
		eval line=$(echo "${line_array[n]}")
		# This should be "3"...
		echo "$line"
	fi
done
# End of working code and return terminal IFS to default...
IFS="$ifs_str"
exit 0

Results...

Last login: Sun Feb 16 16:14:23 on ttys001
AMIGA:barrywalker~> chmod 755 run_var.sh
AMIGA:barrywalker~> ./run_var.sh
00000000  46 49 4c 45 5f 50 41 54  48 7c 7c 7c 7c 24 46 5f  |FILE_PATH||||$F_|
00000010  50 41 54 48 0a 54 41 42  4c 45 5f 4c 49 53 54 7c  |PATH.TABLE_LIST||
00000020  7c 7c 7c 61 7c 62 7c 63  0a 53 59 53 54 5f 4e 4d  ||||a|b|c.SYST_NM|
00000030  7c 7c 7c 7c 24 7b 53 52  43 5f 53 59 53 54 7d 0a  |||||${SRC_SYST}.|
00000040
FILE_PATH||||$F_PATH
TABLE_LIST||||a|b|c
SYST_NM||||${SRC_SYST}
/tmp
3
AMIGA:barrywalker~> _

Or use ENVIRON:

awk -F'|' '
        $NF ~ /\$/ {
                gsub ( /\$|\{|\}/, X, $NF)
                print ENVIRON[$NF]
        }
' abc.dat

@Yoda: works fine if the variable is part of the environment, i.e. it has been exported, not just assigned only.

Here is an example using bash:

$ cat exp_var
#!/bin/bash
F_PATH=/one/two/three
SRC_SYST=apache
IFS='|'
while read a b c d var
do
  if [ "${var:0:1}" = "$" ]
  then
     var=${var:1}
     var=${var%\}}
     var=${var#{}
     echo $var=${!var}
  fi
done< abc.dat

$ ./exp_var
F_PATH=/one/two/three
SRC_SYST=apache