I have a script that contains two shell functions:
runhaskells() {
local DIR=$PWD
local TARGET="cabal.sandbox.config"
while [[ ! -e $DIR/$TARGET -a $DIR != "/" ]]; do
DIR=$(dirname $DIR)
done
if [[ $DIR != "/" ]]; then
local DB=$(sed -ne '/^package-db: */{s///p;q;}' "$DIR/$TARGET")
runhaskell -no-user-package-db -package-db="$DB" "$@"
else
runhaskell "$@"
fi
}
ghcs() {
local DIR=$PWD
local TARGET="cabal.sandbox.config"
while [[ ! -e $DIR/$TARGET -a $DIR != "/" ]]; do
DIR=$(dirname $DIR)
done
if [[ $DIR != "/" ]]; then
local DB=$(sed -ne '/^package-db: */{s///p;q;}' "$DIR/$TARGET")
ghc -no-user-package-db -package-db="$DB" "$@"
else
ghc "$@"
fi
}
which obviously is a repeat of each other except for the fact that it calls a different program in each function. The first function calls the runhaskell
program while the second function calls the ghc
program.
How can I DRY this script (either zsh
or bash
syntax is fine for me) with the end goal that I should be able to call either function at the command line?