In a bash script, I'd like to load settings from a config file and override individual settings with command-line options. In cases where a setting is specified both in the config file and also on the command line, the command-line setting should take precedence.
How do you ensure the config file is loaded before the other getopts blocks? Here's what I've got:
#!/bin/bash
# ...
while getopts “c:l:o:b:dehruwx” OPTION
do
case $OPTION in
c)
echo "load"
CONFIG_FILE=$OPTARG
# load_config is a function that sources the config file
load_config $CONFIG_FILE
;;
l)
echo "set local"
LOCAL_WAR_FILE=$OPTARG
;;
# ...
esac
done
shift $(($OPTIND - 1))
No matter what order I put the handler for the -c option, it always loads the config file AFTER the other options are set. This makes it more of a pain to merge the config file settings with the command-line options.