Frosty555
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:
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?
- 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
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?
Just need the key and value pairs?
awk '/\=/ {print}' /path-to-config-file
awk '/\=/ {print}' /path-to-config-file
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ended up coding it myself.
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 :
Open in new window