You are not logged in.
Pages: 1
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
[git] | [AURpkgs] | [arch-games]
Offline
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
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 Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
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 ). 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
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)
[git] | [AURpkgs] | [arch-games]
Offline
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 Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
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
[git] | [AURpkgs] | [arch-games]
Offline
Pages: 1