Read values from file.

I have a config file of this format:

Company= Alpha Tech
From Email = AlphaTech@Alphatech.com
Pass = Passowrd
To Email = abc@hotmail.com
Smtp=smtp.live.com:587

I want to read these values from this file and use in a command to send email. I am trying grep but it gives full line. I just want those values.

How about a function that invokes sed.

Can I suggest you remove the spaces from the config names to avoid having to use quotes around them.

function param_val {
    sed -n "s/${1}=//p" $2 2> /dev/null
}
 
TO=$(param_val "To Email " /usr/local/lib/config.ini)
PASS=$(param_val "Pass " /usr/local/lib/config.ini)
SMTP=$(param_val Smtp /usr/local/lib/config.ini)

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.

2 Likes

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

@Corona688: Thanks for your reply.

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

Here is config file:

Company=Alpha Tech
FromEmail=test@hotmail.com
Password=Password
ToEmail=abc@live.com
Smtp=smtp.live.com:587

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
1 Like

@Thanks Corona688: This script is working fine on linux CentOS. but when i moved it to ubuntu then i get following error:

Bad substitution

and this error is for following line:

done <  ConfigFile

what is way to fix this issue? and is there any way that i can keep same code for both servers? thanks in advance.

VAR="${VAR// }" does not work in /bin/sh on Ubuntu, neither do arrays nor indirect references. You need to make sure the script is run with bash.

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:

Alpha Tech
test@hotmail.com
Password
abc@live.com
smtp.live.com:587

script:

#!/bin/bash
IFS=$'\n' Values=( "" $(< configfile) )
echo "Company=${Values[1]}"

Or keep support for named params and source the configfile eg:
configfile:

Company="Alpha Tech"
FromEmail="test@hotmail.com"
Password="Password"
ToEmail="abc@live.com"
Smtp="smtp.live.com:587"

script:

#!/bin/bash
. configfile
Values=( "" "$Company" "$FromEmail" "$Password" "$ToEmail" "$Smtp" )
echo "Company=${Values[1]}"