Link to home
Start Free TrialLog in
Avatar of Frosty555
Frosty555Flag for Canada

asked on

Parse config file in bash?

I need my bash script to parse a configuration file. The configuration file needs to hold some specific information:

- a mapping of keys and their values
- a large block of text
- some lines might be comments
- some lines might be whitespace

I imagine one possible format for the config file (although it doesn't have to be done this way) is something like this:

[mapping]

; a comment here
foo=bar
asdf=qwerty

; more comments here
aaa=bbb
banana=orange

[text]
alsidjf alsif jasli fjasli fajslif ja
 ija liaj ilas jalsi jasl
 ijas lifjas lifjas lisja lij

Open in new window



How can I parse something like this in Bash? Do I have to write the parsing myself, or does Bash have some built in config-file-parsing abilities?
Avatar of edster9999
edster9999
Flag of Ireland image

How about you keep it simple and make the config file a script.

so the main script includes it with something like

#!/bin/bash
MY_DIR=`dirname $0`
source $MY_DIR/config.sh

echo "foo is set to $foo and asdf is set to $asdf"

echo -e "The text block is $text"




and the config file is :

# [mapping]
# a comment here

foo="bar"
asdf="qwerty"

# more comments here
aaa="bbb"
banana="orange"

# [text]
text="alsidjf alsif jasli fjasli fajslif ja\n\
 ija liaj ilas jalsi jasl\n\
 ijas lifjas lifjas lisja lij\n"

Open in new window

Just need the key and value pairs?

awk '/\=/ {print}'  /path-to-config-file
ASKER CERTIFIED SOLUTION
Avatar of Frosty555
Frosty555
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Frosty555

ASKER

Ended up coding it myself.