Sponsored Content
Top Forums Shell Programming and Scripting How to parse config variables from external file to shell script Post 302125948 by aigles on Monday 9th of July 2007 08:24:55 AM
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

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
ExtUtils::Config(3)					User Contributed Perl Documentation				       ExtUtils::Config(3)

NAME
ExtUtils::Config - A wrapper for perl's configuration VERSION
version 0.007 SYNOPSIS
my $config = ExtUtils::Config->new(); $config->set('installsitelib', "$ENV{HOME}/lib"); DESCRIPTION
ExtUtils::Config is an abstraction around the %Config hash. METHODS
new(\%config) Create a new ExtUtils::Config object. The values in "\%config" are used to initialize the object. get($key) Get the value of $key. If not overriden it will return the value in %Config. exists($key) Tests for the existence of $key. set($key, $value) Set/override the value of $key to $value. clear($key) Reset the value of $key to its original value. values_set() Get a hashref of all overridden values. all_config() Get a hashref of the complete configuration, including overrides. clone() Clone the current configuration object. serialize() This method serializes the object to some kind of string. AUTHORS
o Ken Williams <kwilliams@cpan.org> o Leon Timmermans <leont@cpan.org> COPYRIGHT AND LICENSE
This software is copyright (c) 2006 by Ken Williams, Leon Timmermans. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.18.2 2012-10-17 ExtUtils::Config(3)
All times are GMT -4. The time now is 03:01 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy