#1/bin/bash
PKG_MANAGER="paludis"
typeset -a OverlayKeys
typeset -a OverlayVals
OverlayKeys[0]="foobar"
OverlayVals[0]="http://www.someurl.com/file.xml"
OverlayKeys[1]="mpd"
OverlayVals[1]=""
function _overlay_command() {
key="${1}" && val="${2}"
if [ -z "${val}" ]; then
case $PKG_MANAGER in
paludis)
playman -a ${key}
paludis -s x-${key}
;;
emerge)
layman -a ${key}
layman -s ${key}
;;
esac
else
case $PKG_MANAGER in
paludis)
playman --layman-url ${val} -a ${key}
paludis -s x-${key}
;;
emerge)
layman -f -o ${val} -k -a ${key}
layman -s ${key}
;;
esac
fi
}
function _loop_overlay() {
for (( x=0 ; x < ${#OverlayKeys[@]}; x++ ))
do
if [ $key == "${OverlayKeys[$x]}" ]
then
_overlay_command "${key}" "${OverlayVals[$x]}"
fi
done
}
function setup_overlay() {
for _key in ${OverlayKeys[@]}
do
key="${_key}" _loop_overlay
done
}
setup_overlay
Refactorings
No refactoring yet !
Matt
November 14, 2009, November 14, 2009 10:43, permalink
Looks like you only need one loop
#!/bin/bash
PKG_MANAGER="paludis"
keys=("foobar" "mpd")
vals=("http://www.someurl.com/file.xml" "")
for (( x=0 ; x < ${#keys[*]}; x++ ))
do
case $PKG_MANAGER in
paludis)
# execute first command
if [ -z ${vals[$x]} ]; then
echo playman -a ${keys[$x]}
else
echo playman --layman-url ${vals[$x]} -a ${keys[$x]}
fi
# execute second command
echo paludis -s x-${keys[$x]}
;;
emerge)
# execute first command
if [ -z ${vals[$x]} ]; then
echo layman -a ${keys[$x]}
else
echo layman -f -o ${vals[$x]} -k -a ${keys[$x]}
fi
# execute second command
echo layman -s ${key}
;;
esac
done
gregf
November 14, 2009, November 14, 2009 15:24, permalink
Thanks a lot, 100x simpler than what I had going. Thanks. :)
Matt
November 14, 2009, November 14, 2009 18:34, permalink
Yep... :)
looking at it again there looks to be a bug in line 31 though
should be ${keys[$x]}
Working on a script, took out relevant parts for this. Got a big associative array I'm looping through, and running a few shell commands based on a package manager. It works fine, but she's a bit fugly and I"m not much of a bash guy. Any help prettying up would be appreciated.