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). 


Monday, July 7, 2014

which and alias: which doesn't consider aliases......use type not which

Have you notive that "which cmd" doesn't give your alias path if cmd is in your aliases?

Aliases are checked before paths when you try to run a cmd in a prompt but which doesn't consider aliases. Which search for the cmd in your paths. If cmd is in your path, you will be confused and you might turn crazy as I close did.

Use type instead of which.

usage: which [options] [--] COMMAND [...]
             Write the full path of COMMAND(s) to standard output.

usage: type [-aftpP] name [name ...]
With no options, indicate how each name would be interpreted if used as a command name