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
Mono(mconfig)															     Mono(mconfig)

NAME
mconfig, - Utility for modifying .NET configuration files SYNOPSIS
mconfig [options] command [command_parameters] DESCRIPTION
mconfig can be used to edit .NET configuration files, by adding "features" (that is sets of xml statements) defined in one of the config files read by mconfig. The config file can also define layouts of default configuration files, which may be useful for bootstrapping your .NET projects. To see the list of recognized commands, default configuration files and features, run mconfig without passing any parameters. OPTIONS
-c <config_file>, --config=<config_file> Read the specified config file after reading the other, preconfigured, config files for the utility. Settings in the specified file override those found in the other configuration locations. -t {any | web | application}, --target={any | web | application} Features and default configuration files defined in the mconfig config file(s) can be assigned to one of the three targets - web (for ASP.NET features/config files), application - for .NET applications, any - applicable in both of the previous targets. Defaults to any. -?, -h, --help Show a summary usage screen. -v, --version Show the mconfig version COMMANDS
{addfeature, af} <feature_name> [config_file_path] Adds the feature named <feature_name> to the specified config file. If [config_file_path] is omitted, the name of the output config- uration file will be chosen based on the selected target (see the -t option). The web target outputs configuration to file named Web.config, and the application target outputs to file named application.exe.config. The any target does not have a default output file. If the specified config file exists, the feature will be injected into it at the locations specified by mconfig configuration. If the target config file does not exist, it will be created and will contain only the specified feature and all its dependencies. {defaultconfig, dc} [config_name [target_directory]] Generates a default config file using the configuration entry named [config_name] and outputs the resulting configuration file to the directory given by the [target_directory] option. If [config_name] is omitted, it defaults to Web.config for the web target and application target. The any target does not specify any default output configuration name. If the [target_directory] parameter is omitted, it defaults to the current directory. Name of the output config file created in [target_directory] is given in the config file layout definition. FILES
Config files are read in the order given below. Each subsequent file may override settings found in the files read before it. $prefix/etc/mono/mconfig/config.xml The default configuration file, distributed with mconfig. $prefix is the mconfig installation prefix specified on the compilation time. $config_dir/mconfig/config.xml $config_dir is the directory specified in the XDG_CONFIG_HOME environment variable or, if it is empty, in the .config directory located in the user's home directory. This file is not distributed with Mono. ./mconfig.xml Local configuration file which can contain per-application settings. SEE ALSO
mconfig.config (5) AUTHOR
Written by Marek Habersack COPYRIGHT
Copyright (C) 2007 Novell, Inc (http://www.novell.com) MAILING LISTS
Visit http://lists.ximian.com/mailman/listinfo/mono-devel-list for details. WEB SITE
Visit: http://www.mono-project.com for details Mono(mconfig)
All times are GMT -4. The time now is 11:00 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy