Read values from file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read values from file.
# 1  
Old 04-19-2012
Read values from file.

I have a config file of this format:
Code:
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.
# 2  
Old 04-19-2012
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.

Code:
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)

# 3  
Old 04-19-2012
You can use a shell loop to do this without running sed 6 times to process one file...

Code:
#!/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.
These 2 Users Gave Thanks to Corona688 For This Post:
# 4  
Old 04-19-2012
@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.
# 5  
Old 04-20-2012
@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.

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

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

# 6  
Old 04-20-2012
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.

Code:
while IFS="=" read VAR VALUE
do
        VAR="${VAR// }"
        Values[$i]=${VALUE}
        let i++;
done <  ConfigFile

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 04-21-2012
@Thanks Corona688: This script is working fine on linux CentOS. but when i moved it to ubuntu then i get following error:

Code:
Bad substitution

and this error is for following line:

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read a file and replace values in a script

Hi , I have a property file placed in folder /usr/opt/temp/aorc.prop which has values given below . I need to read this file content and replace the node with actual values in a shell script . Each time the script shall replace the node value from the poperty file and execute a cfsend command and... (10 Replies)
Discussion started by: samrat dutta
10 Replies

2. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

3. Shell Programming and Scripting

Read values from second file in awk

Hello Friends, I have written a script like the following, which finds some logs and fetchs some desired rows and then calculate average processing time of a spesific application. if then echo echo "----- There are three arguments which is expected to run this script! ... (5 Replies)
Discussion started by: EAGL€
5 Replies

4. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

5. Shell Programming and Scripting

How to Read different values from external file?

Hi , I am new to this scripting , I am facing an issue like how to read different values from external file by using different variables, In script I supposed to declare var 1 var 2 var 3 I know how to call this variables from external file (I am using awk command by giving same... (3 Replies)
Discussion started by: chandini
3 Replies

6. Shell Programming and Scripting

Read variables and their values from file

Hi, I want to read the variables and the values from the txt file and compare these values with the ones computed by script. for ex: say var.txt contains the variable names and their values: one 1 two 2 three 3 The value of variables "one" "two" and "three" will be computed in the script... (3 Replies)
Discussion started by: bhushana
3 Replies

7. Shell Programming and Scripting

Read values from a file

Hi , I have a file with the following content I need the read the year and reporting from this file and store them in variables. I understand that we can read the file delimited by'=' but not sure how to extract the values correctly. Thanks in advance Regards (3 Replies)
Discussion started by: w020637
3 Replies

8. Shell Programming and Scripting

how to read only the first two values from a file in unix

Hi, I'm new to unix.. I need to read only the first two values from a file that contains many data....My record is like this FTG_ver_num=7.0 RUN_ID=2 $$tgt_envrnmt_cd=FTG $$WRK_FLW_CD=NPL I need oly the values of ETG_ver_num and RUN_ID. i.e) output to be 7.0 and 2 The code i used... (2 Replies)
Discussion started by: raghulshekar
2 Replies

9. Shell Programming and Scripting

how do I pass or read values from file ?

Hi one & All , My Need is to Create 64 Partition and create File System in Linux. I have the Script ... for((a=0;a<=63;a++)) do fdisk /dev/cciss/c0d$a done for((a=0;a<=63;a++)) do mkfs.ext2 /dec/cciss/'c0d'$a'p1' done the moment I run the Script I get the Prompt ... Command... (1 Reply)
Discussion started by: nix-kid
1 Replies

10. UNIX for Dummies Questions & Answers

How can I read variable values from file?

Hi, I want to pratmeterze my scripts like, my confRsync file contains varibale values for 1. host 2. Destination and 3. source like this, I want to read this values from this and assing to my makeRsyn.sh file's varibales. how to do this? (1 Reply)
Discussion started by: redlotus72
1 Replies
Login or Register to Ask a Question