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:
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
$ 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