Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Saturday, July 19, 2014

Simple way to make bash script configurable

In order to simplify testing, I needed to make a bash script configurable. 
Yes bash is quite convenient to archive some tasks. 
I had trouble finding the syntax to override environment variable.

Here is a simple way to make to do it:

  • set default environment variables (recommendation = pointing to stable code)
  • make override config file a parameter 
  • add this to make config override working (config is argument 4):

# check override configuration
if [ ! -z $4 ]
then
    echo "load config file:" $4
    source $4
else
    echo "default config (stable branch)"
fi

for VARIABLE in PYTHON INPUT OUTPUT
do
    eval var=\\$VARIABLE
    [ -z ${!var} ] && { echo "Need to set $VARIABLE"; exit 1; }
    echo $VARIABLE":"${!var}
done

example override_config.sh:
PYTHON=/work/fpieraut/py27/bin/python

Yes the syntax is a little cryptic but that is the only way I found to make is working;)
Btw, this isn't working in sh (calling source to set variables).