#!/bin/bash -e
ARGUMENT_LIST=(
"arg-one"
"arg-two"
"arg-three"
)
# read arguments
opts=$(getopt \
--longoptions "$(printf "%s:," "${ARGUMENT_LIST[@]}")" \
--name "$(basename "$0")" \
--options "" \
-- "$@"
)
eval set --$opts
while [[ $# -gt 0 ]]; do
case "$1" in
--arg-one)
argOne=$2
shift 2
;;
--arg-two)
argTwo=$2
shift 2
;;
--arg-three)
argThree=$2
shift 2
;;
*)
break
;;
esac
done
Note
The eval
in eval set --$opts
is required as arguments returned by getopt
are quoted.
$ ./getopt.sh --arg-one "apple" --arg-two "orange" --arg-three "banana"
Its not worked with empty arguments. If we run:
$ ./getopt.sh --arg-one "apple" --arg-two --arg-three "banana"
we will be have
argTwo=--arg-three