Help with dynamic configure cfg files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with dynamic configure cfg files
# 1  
Old 05-20-2010
Wrench Help with dynamic configure cfg files

hi; i have one configuration file(configuration.cfg),where contents are below..
Code:
filename = charge.cfg
sectionname = [AUTORETRYTIMES]
networkid[1] = 1
retrytimes[1] = 2
-------------
--------------
sectionname = [CHARGE]
networkid[2] = 1
retrytimes[2] = 2
filename = xyz.cfg
------------------
-----------------

There is multiple filename & section name's.
How i read filename and section name and also between lines in two diff. sections dynamically ?
Then search as per filename in config directory,if found,then search for section name,if found then append/insert the lines in that file in shell script(linux).

Last edited by Scott; 05-20-2010 at 03:02 PM.. Reason: Please use code tags
# 2  
Old 05-20-2010
welcome to the forum.

Your order is not fixed. Is that true?

for the first set, you have filename then section name.
in that later, first you have section name and filename in last?!!
# 3  
Old 05-20-2010
Should this act as you want?
Code:
#!/bin/bash
read -p "Section name: " SECTION
read -p "File name: " FILE
until [ "$L" = "filename = $FILE" ]; do read L; done # seek file name
until [ "$L" = "sectionname = [$SECTION]" ]; do read L; done # seek section name
IFS='='
while read A B
do
    [ "$VAR" = filename ] || [ "$VAR" = sectionname ] && break # if next file or section encountered
    eval "$A=${B:1}" # ${B:1} to cut the first char (space)
done < configuration.cfg



---------- Post updated at 19:55 ---------- Previous update was at 19:42 ----------

If there were no spaces around the '=', you could simply do somethin like
Code:
read
eval "$REPLY"


Last edited by frans; 05-20-2010 at 03:53 PM.. Reason: space problem
# 4  
Old 05-21-2010
Actually anchal in configure.cfg file,so many filenames are there and under one filename so many sectionname's,and under each section so many parameter's.
# 5  
Old 05-21-2010
from the input file, what's the output you expect?

if the session grouped by "----------------------"
# 6  
Old 05-21-2010
Quote:
Originally Posted by rdcwayx
from the input file, what's the output you expect?

if the session grouped by "----------------------"
Hi rdcwayx;
Actually i am expecting filename first from input file and search in bin folder for that filename,if found,then expecting sectionname from input file and again search that section available in that filename,if found,then iam expecting all parameter's from input file(parameter's are reside in input file is like this,may be in between two sections || may be in between sectionname and filename || section name and eof),then i am appending/inserting those parameter's in that section(destination file's section) .
Again in destination file,many probality,means taht file consist only many || one sectionname(like [charge]) and parameter's.when we found sectionname from destination file,we have to check the position where we insert/append parameter's taking from input file.again we encounter multiple section and finally eof in destination file.

---------- Post updated at 12:12 AM ---------- Previous update was at 12:02 AM ----------

Quote:
Originally Posted by frans
Should this act as you want?
Code:
#!/bin/bash
read -p "Section name: " SECTION
read -p "File name: " FILE
until [ "$L" = "filename = $FILE" ]; do read L; done # seek file name
until [ "$L" = "sectionname = [$SECTION]" ]; do read L; done # seek section name
IFS='='
while read A B
do
    [ "$VAR" = filename ] || [ "$VAR" = sectionname ] && break # if next file or section encountered
    eval "$A=${B:1}" # ${B:1} to cut the first char (space)
done < configuration.cfg

---------- Post updated at 19:55 ---------- Previous update was at 19:42 ----------

If there were no spaces around the '=', you could simply do somethin like
Code:
read
eval "$REPLY"

Hi frans;
I am not expecting input from console,input is file(configure.cfg) and changes will happen in bin/filename,comes from configure.cfg.
# 7  
Old 05-21-2010
Maybe, this should do the job (take care : it modifies the config files (make a backup))
Code:
#!/bin/bash
while read A S B
do
    case $A in
        filename)    # process new file
            FILE=bin/$B
            unset SECT
            [ -f $FILE ] || { echo "$FILE not found"; unset FILE; }
        ;;
        sectionname) # new section
            [ -z "$FILE" ] && continue
            SECT="$B"
            # return the line number of section in $FILE
            N=$(grep -Fwn "$SECT" $FILE | cut -d':' -f1) || { echo "$SECT not found in $FILE "; unset SECT; }
        ;;
        *)    # process parameter
            if [ -n "$FILE" ] && [ -n "$SECT" ]
            then
                ((N++))
                sed -i "${N}i\n$A $S $B" $FILE
            fi
        ;;
    esac
done < configuration.cfg

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Kickstart using anaconda-ks.cfg

Heyas I'm trying to build a live image using livecd-tools (livecd-creator), but this time i'd like to reuse the /root/anaconda-ks.cfg. System: Fedora 3.16.7-200.fc20.x86_64 The building starts, the download begins, but it fails with networking. MAIN.ks # Based on: fedora-live-base.ks... (0 Replies)
Discussion started by: sea
0 Replies

2. Shell Programming and Scripting

Help Dynamic looping based on files

Hi I have to run the script (a part of the code) in a loop for the no of times the files present in the directory, by taking one file and process and next another file. For example, if we do ls and the result have: $ls abc.dat def.dat ghi.dat The script code should loop for 3... (4 Replies)
Discussion started by: karumudi7
4 Replies

3. Solaris

Configure network in unix with dynamic ip on my DHCP server?

can anyone tell me how can i configure network in unix with dynamic ip on my DHCP server?? (7 Replies)
Discussion started by: Reham.Donia
7 Replies

4. Red Hat

Help in ANACONDA-ks.cfg

i have automated installation of my proprietory linux.. using PXE. it all goes well. but after complete installation, while using the admin interface.. i am facing display problems. is it due to package dependencies.? or i have to write post script in anaconda-ks.cfg file??? can some one tell... (10 Replies)
Discussion started by: jayesh_j
10 Replies

5. Shell Programming and Scripting

Help - Using Find command on dynamic files on KSH

Hi Forum. When I run the following find command, I get the desired results: find . \( -name a.out -o -name '*.o' -o -name 'core' \) -type f -ls I want for my script to dynamically calculate and assign a variable var1 to contain all the files that I want to search instead of hard-coding. ... (2 Replies)
Discussion started by: pchang
2 Replies

6. BSD

configure /usr/local/etc/be-agent.cfg

hi Howto configure Ralus at /usr/local/etc/be-agent.cfg on freeBSD 7.0 for Backup Exec 9.1? I've tried the following: # cat /usr/local/etc/be-agent.cfg name bsd.domain export / as root export /usr as usr export /var as var export /tmp as tmp force_address 10.10.1.50 tell... (0 Replies)
Discussion started by: ccc
0 Replies

7. UNIX for Advanced & Expert Users

Dynamic Downloading and executing of ELF files

Dear Group, I want to prepare an ELF file which can be downloaded dynamically to any address in the moemory and executes as a new task/thread/process. 1) for this what are all the compileation, linker options while building the ELF file? 2) which parts of ELF file has to modified while... (1 Reply)
Discussion started by: ravinder.are
1 Replies

8. UNIX for Dummies Questions & Answers

Problem with shared dynamic library files

I am having a major problem. Most of the commands that i am running on my centos 5 system is giving the error of type: <dynamic shared library file>:open failed: No such file or directory For example: libgcc_s.so.1: open failed: No such file or directory How can i solve this? (6 Replies)
Discussion started by: proactiveaditya
6 Replies

9. HP-UX

dynamic names for .tar.gz files?

Hi All, I have a requirement to zip and compress files with dynamic names (which includes date and time component in their names) needs to be compressed with same name. for example I have a file T.30.SLS_ORD.SO.27.2_2_2.20080623.041415.dat which needs to archived as... (1 Reply)
Discussion started by: srinivas_paluku
1 Replies

10. UNIX for Advanced & Expert Users

How to Read config (.cfg) files using shell scripting

Hello Friends I am new to this forum and this is my first post. I have to write a script in which i have to read data from a configuration ( .cfg) file. And also i have to execute/call some already written scripts from this script. Can you people plpease help me on this regards. Thanks... (5 Replies)
Discussion started by: smallwonder
5 Replies
Login or Register to Ask a Question

Featured Tech Videos