How to parse config variables from external file to shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to parse config variables from external file to shell script
# 1  
Old 07-08-2007
How to parse config variables from external file to shell script

How do i use a config.txt to recursively pass a set of variables to a shell script

eg my config.txt looks like this :

[set_1]
path=c://dataset/set1
v1= a.bin
v2= b.bin

[set_2]
path=c://dataset/set2
v1= xy.bin
v2= abc.bin

[set_3]
..................

and so on .

and my testscript :

#!/bin/bash
.$1

echo $path $v1 $v2


Right now I am able to parse first set of variables . How do i move on to the subsequent set_2 , set_3 ...........


Thanks in advance
pradsh
# 2  
Old 07-09-2007
You can do something like that :
Code:
# Function: get_config_list config_file
          # Purpose : Print the list of configs from config file
get_config_list()
{
   typeset config_file=$1

   awk -F '[][]' '
      NF==3 && $0 ~ /^\[.*\]/ { print $2 }
   ' ${config_file}
}

# Function : set_config_vars config_file config [var_prefix]
# Purpose  : Set variables (optionaly prefixed by var_prefix) from config in config file
set_config_vars()
{
   typeset config_file=$1
   typeset config=$2
   typeset var_prefix=$3
   typeset config_vars

   config_vars=$( 
        awk -F= -v Config="${config}" -v Prefix="${var_prefix}" '
        BEGIN { 
           Config = toupper(Config);
           patternConfig = "\\[" Config "]";
        }
        toupper($0)  ~ patternConfig,(/\[/ && toupper($0) !~ patternConfig)  { 
           if (/\[/ || NF <2) next;
           sub(/^[[:space:]]*/, "");
           sub(/[[:space:]]*=[[:space:]]/, "=");
           print Prefix $0;
        } ' ${config_file} )

   eval "${config_vars}"
}

#
# Set variables for all config from config file
#
file=config.txt
for cfg in $(get_config_list ${file})
do
   echo "--- Configuration [${cfg}] ---"
   unset $(set | awk -F= '/^cfg_/  { print $1 }') cfg_
   set_config_vars ${file} ${cfg} cfg_
   set | grep ^cfg_
done

Input file (config.txt)
Code:
[set_1]
path=c://dataset/set1
v1= a.bin
v2= b.bin

[set_2]
path=c://dataset/set2
v1= xy.bin
v2= abc.bin

[set_3]
v1 = value1

Output:
Code:
--- Configuration [set_1] ---
cfg_path=c://dataset/set1
cfg_v1=a.bin
cfg_v2=b.bin
--- Configuration [set_2] ---
cfg_path=c://dataset/set2
cfg_v1=xy.bin
cfg_v2=abc.bin
--- Configuration [set_3] ---
cfg_v1=value1

# 3  
Old 07-09-2007
Since you aren't worried about accessing the First Set of Variables again, you can just source the second set... Using your example:

[set_1]
path=c://dataset/set1
v1= a.bin
v2= b.bin

[set_2]
path=c://dataset/set2
v1= xy.bin
v2= abc.bin

[set_3]
..................

and so on .

and my testscript :

#!/bin/bash
.$1 #Here you are sourcing set 1 - I assume

echo $path $v1 $v2 #here you are showing the variables

. $2 #Here you are sourcing set 2
echo $path $v1 $v2 #here you are showing the new variables

# Just make sure to call the script with the variable files as the first and second argument.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

2. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

3. Shell Programming and Scripting

Parse variables from C++ to shell

Hi All, #include <iostream> int main() { std::int foo = 34; system("mkdir /home/linuxUser/fooDir"); system("vi fooFile") system("write foo in fooFile") foo = 43; foo = read foo from fooFile; std::cout << "foo = " << foo ; } result should be foo = 34 can... (3 Replies)
Discussion started by: linuxUser_
3 Replies

4. Shell Programming and Scripting

Getting category when given the variable from external file to shell script

Hi, I have a script that interacts with a config file in the format: file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt I would like to return the Category, when given the file name. (11 Replies)
Discussion started by: MoreCowbell
11 Replies

5. Shell Programming and Scripting

Shell script that will compare two config files and produce 2 outputs 1)actual config file 2)report

Hi I am new to shell scripting. There is a requirement to write a shell script to meet follwing needs.Prompt reply shall be highly appreciated. script that will compare two config files and produce 2 outputs - actual config file and a report indicating changes made. OS :Susi linux ver 10.3. ... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

6. Shell Programming and Scripting

Parse config file data to script variable

I have a script with few pre defined variables. Also have a config file. Something like this. # config file # Define Oracle User MOD1_TAG=abcde MOD2_TAG=xyzabc MOD3_TAG=def I need to parse the config file and have each of the value copied to different variables. Please suggest what... (1 Reply)
Discussion started by: souryadipta
1 Replies

7. Shell Programming and Scripting

Parse config file and store the values in variables

Hi, I have a config file that has blank, commented lines. I need to escape commented lines, blank lines, parse the remaining lines and store them in variables or array. the config file contains the following lines. # config file # Define Oracle User ORA_USER=abcde ORA_PASS=xyzabc... (8 Replies)
Discussion started by: Lakshmi Chowdam
8 Replies

8. Shell Programming and Scripting

sed command to parse Apache config file

Hi there, am trying to parse an Apache 'server' config file. A snippet of the config file is shown below: ..... ProxyPassReverse /foo http://foo.example.com/bar ..... ..... RewriteRule ^/(.*) http://www.example.com/$1 RewriteRule /redirect https://www.example1.com/$1 ........ (7 Replies)
Discussion started by: jy2k7ca
7 Replies

9. Shell Programming and Scripting

how to access variables in a config file inside a shell script

I'm writing a shell script. I want to put the variables in a separate config files and use those inside my script. e.g. the config file (temp.conf)will have the values like mapping=123 file_name=xyz.txt I want to access these variables in temp.conf(i.e. mapping and file_name) from inside the... (7 Replies)
Discussion started by: badrimohanty
7 Replies

10. Shell Programming and Scripting

Edit a config file using shell script

I need to edit a config file using shell script. i.e., Search with the 'key' string and edit the 'value'. For eg: below is what I have in the config file "configfile.cfg". Key1=OldValue1 Key2=OldValue2 I want to search for "Key1" and change "OldValue1" to "NewValue1" Thanks for your... (7 Replies)
Discussion started by: rajeshomallur
7 Replies
Login or Register to Ask a Question