This might be an obvious error but I'm just not seeing it.
I have a case statement which is supposed to enable passing arguments to a script (ex: script.sh --verbose --all).
For some reason, this specific function does not work correctly - it only works on whichever flag I pass first:
#Pass arguments to the script
flags()
{
while test $# -gt 0
do
case "$1" in
(-v|--verbose)
shift
export VERBOSE="1"
shift;;
(-vv|--extra-verbose)
shift
export VERBOSE="1"
export EXTRA_VERBOSE="1"
shift;;
(-a|--all)
shift
export ALL="1"
shift;;
(-h|--help)
usage;;
(*) usage;;
esac
done
}
flags "$@"
echo "Verbose: $VERBOSE, Extra Verbose: $EXTRA_VERBOSE, All: $ALL"
So in this case, if I pass -v -a, the script will only set the $VERBOSE variable. If I pass -a -v, the script will only set the $ALL variable.
I have quite a few scripts using this structure which do not exhibit this.
I'd really appreciate it if you could help me figure out what is it I'm missing.
Thanks!
shifttwice?shifts?shiftremoves one value from the start of the list-v -a,$1is-vand$2is-a. After ashift,$1is-a. After the secondshift,$#is 0.exportevery variable.