Solved

Parse config file in bash?

Posted on 2011-09-07
4
615 Views
Last Modified: 2012-06-21
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?
0
Comment
Question by:Frosty555
  • 2
4 Comments
 
LVL 20

Expert Comment

by:edster9999
Comment Utility
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

0
 
LVL 38

Expert Comment

by:wesly_chen
Comment Utility
Just need the key and value pairs?

awk '/\=/ {print}'  /path-to-config-file
0
 
LVL 31

Accepted Solution

by:
Frosty555 earned 0 total points
Comment Utility
Hi guys,

I didn't want to make it a bash script because it needs to be edited and created by people who do not necessarily know bash. Wesly, your awk line leaves the comments in and doesn't trim any spaces.

I ended up parsing the file through a much larger bash script that was essentially a state machine that looped through it line by line and split out the parts that it needed.

My requirements for the mapping file changed a little over the course of this project. I now needed to have a [mapping], [postupgrade] and [preupgrade] sections.

Here's how I ended up doing it

    # parse the mapping file
    mode=none
    rm -f /tmp/postupgrade.sql /tmp/preupgrade.sql /tmp/mapping.txt /tmp/upgrade.sql

    cat "$mapfile" | while read line; do

        # trim whitespace off the beginning and end of the line
        line=`echo "$line" | awk '{ gsub(/^[ \t]+|[ \t]+$/, ""); print }'`

        # change mode
        if [ "$line" = "[mapping]" ]; then
            mode=mapping
            #echo "Entered mapping mode"
            continue
        fi
        if [ "$line" = "[postupgrade]" ]; then
            mode=postupgrade
            #echo "Entered postupgrade mode"
            continue
        fi
        if [ "$line" = "[preupgrade]" ]; then
            mode=preupgrade
            #echo "Entered preupgrade mode"
            continue
        fi

        case "$mode" in
            mapping)
                if [ "${line:0:1}" == ";" ] || [ -z "$line" ]; then
                    :
                    #echo "discarded line: $line"
                else
                    echo "$line" >>/tmp/mapping.txt
                    #echo "mapping line: $line"
                fi
                ;;

            postupgrade)
                echo "$line" >>/tmp/postupgrade.sql
                #echo "postupgrade line: $line"
                ;;

            preupgrade)
                echo "$line" >>/tmp/preupgrade.sql
                #echo "preupgrade line: $line"
                ;;

            *)
                #echo "discarded line: $line"
        esac

Open in new window


Then I followed a different expert's suggestion in another question for how exactly to parse the mapping.txt file further using awk.
0
 
LVL 31

Author Closing Comment

by:Frosty555
Comment Utility
Ended up coding it myself.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now