You are not logged in.

#1 2008-08-04 19:22:49

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Pacman.conf parser for bash

I was thinking about the best way to parse pacman.conf for a bash script, and I was wondering if anyone had come up with something. If noone has, I was thinking of writing a perl script to do it:

$ pacparser.pl
pacman_repos=('core' 'extra' 'community' 'archlinuxfr')
pacman_mirrors=('ftp://ftp.archlinux.org/core/os/i686' [...]
eval `pacparser.pl`
for repo in ${pacman_repos[@]}; do
foo

Offline

#2 2008-08-04 22:02:52

Husio
Member
From: Europe
Registered: 2005-12-04
Posts: 359
Website

Re: Pacman.conf parser for bash

Isn't it like *.ini file? I'm sure ConfigParser can read it. It's Python module. But plain bash might be better choice, so why don't just parse the file?

 # sed /etc/pacman.conf  -n -e 's/\(.*\)\b.*=[\ ]*\(.*\)/\1="\2"/p'

Last edited by Husio (2008-08-04 22:08:59)

Offline

#3 2008-08-04 22:08:39

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: Pacman.conf parser for bash

Take a look at the code in powerpill. To load the settings from pacman.conf, I had to write a parser that could follow the includes. Maybe you could reuse it for what you have in mind.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#4 2008-08-05 00:40:38

chimeric
Member
From: Munich, Germany
Registered: 2007-10-07
Posts: 254
Website

Re: Pacman.conf parser for bash

Hi,

I once wrote a config parser in bash for a slighty different config file style, but I got it adapted to .ini style files pretty quickly. It skips comments and whitespace, but it doesn't support inline comments (I am too sleepy atm to add that, maybe tomorrow  wink). The following code includes the functions and a usage example. Maybe it's of use for you.

#!/bin/bash

CONFIG=/etc/pacman.conf

function readconf() {
 
    match=0
 
    while read line; do
        # skip comments
        [[ $line =~ ^\ {0,}# ]] && continue

 
        # skip empty lines
        [[ -z "$line" ]] && continue
 
        # still no match? lets check again
        if [ $match == 0 ]; then

            # do we have a section tag ?
            if [[ $line =~ ^\[.*?\] ]]; then

                #strip []
                line=${line:1:$((${#line}-2))}
                # strip whitespace
                section=${line// /}

                # do we have a match ?
                if [[ "$section" == "$1" ]]; then
                    match=1
                    continue
                fi
 
                continue
            fi
 
        # found next section after config was read - exit loop
        elif [[ $line =~ ^\[.*?\] && $match == 1 ]]; then
            break
 
        # got a config line eval it
        else
            var=${line%%=*}
            var=${var// /}
            value=${line##*=}
            value=${value## }
            eval "$var='$value'"
        fi
 
    done < "$CONFIG"
}


readconf options
echo $HoldPkg
echo $section

readconf core
echo $Include

PS.: At the moment it works on a per section basis, but this could as well be wrapped up to read the pacman.conf only once.

Last edited by chimeric (2008-08-05 00:52:18)

Offline

#5 2008-08-05 17:39:58

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Pacman.conf parser for bash

Thanks for the support everyone! Does anyone have any objection to me including this parsing code in pkgtools? Currently it's under the GPL.

EDIT: Looks like powerpill is GPL also, so I'll include it now. Thanks!

Last edited by Daenyth (2008-08-05 18:02:14)

Offline

#6 2008-08-05 17:51:03

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: Pacman.conf parser for bash

Feel free to pick and choose from powerpill, it's currently under GPLv2. Let me know if you would like any help in adapting it to suit your objectives.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#7 2008-08-05 18:03:50

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Pacman.conf parser for bash

Thanks for the help with it. I'll use it as a starting point. I'm fairly handy with perl so I doubt I'll run into anything too scary smile

Offline

Board footer

Powered by FluxBB