Need help parsing config file in ksh

Hi all, I've done some searching here but haven't found exactly what I'm looking for so I thought I'd post up and see if someone can help out.

I'm working on a shell script that I would like to store environment variables in an external file. I'm familiar with sourcing a file with variables in it, but I was looking for the ability to just source a section of the config file depending on what part of the script is being run.

For example:

sample.conf:

[default]
INST_DIR="/root/scripts"

[os]
OS_TYPE="Linux"

OR

sample2.conf:

default {
INST_DIR="/root/scripts"
}

os {
OS_TYPE="Linux"
}

The config file doesn't need to follow these examples if you have a method that supports sections.

The plan is to be able to call the code that parses each section as a function:

In example:

readconfig "os"
echo ${OS_TYPE}

Then any and all environment variables in the "os" section are available to the script needing them.

Ideally this needs to be in Korn (ksh) shell to be cross server compatible. I need this to work with Linux, Solaris, HP-UX and AIX. If I can't get this working it isn't a game killer, but it would be nice rather than the alternative.

Thanks in advanced!

If you want your script to be portable, you should use POSIX syntax, not ksh.

Example config file:

section=a
x=1
y=2
z=3
section=b
z=-1
y=666
y=13
section=c

Use section $1:

eval "$(sed -n "/section=$1/,/section=/p" "$file")"