You can use a shell loop to do this without running sed 6 times to process one file...
#!/bin/bash
exec 5<configfile
IFS="="
while read VAR VALUE
do
echo "'${VAR}' => '${VAR// }'"
if [ -z "$VALUE" ]
then
read G <&5
else
VAR="${VAR// }"
read G "${VAR}" <&5
echo "${VAR} is '${!VAR}'"
fi
done <configfile
This will set shell variables like $Company, $ToEmail, $Smtp, etc.
@kashif.live
Warning: If you continue to post vague, inaccurate and badly typed posts and without mentioning what Operating System and version you are running (and what Shell you are using), you will be summarily banned from this site.
Pass = Passowrd
Syntactically incorrect in unix and with obvious typing errors.
I have written this code to read values form config file and then use them at later stage in an array. Kinldy review it and suggest if I'm not doing extra.
exec 5< ConfigFile
declare -a Values
IFS="="
i=0
while read VAR VALUE
do
if [ -z "$VALUE" ]
then
read G <&5
else
VAR="${VAR// }"
read G "${VAR}" <&5
Values[$i]=${!VAR}
let i++;
fi
done < ConfigFile
There's no point going to all that trouble to set variables of the right name if all you're going to do is put them in an associative array after. Read the code, try to understand what it's doing, don't just copy it blindly.
Only extremely specific shells are going to have associative arrays available.
while IFS="=" read VAR VALUE
do
VAR="${VAR// }"
Values[$i]=${VALUE}
let i++;
done < ConfigFile
As Scrutinizer says use bash if you want arrays, etc. Put #!/bin/bash on the first line assuming you have bash installed on CentOS.
But let's get back to the array, With the code you are curently using anything before the "=" in the configfile is irrelevent: line 1 will go into array element #1 line 2 in #2, etc. Having said that you could simpify things by having your config file with just the values:
configfile: