I'm trying to create a bash script which reads a set of optional input parsing parameters. I tried to follow this example on GitHub.com.
I tried to add another integer parameter to read, which I called x
(or xxx-size
). I thought it was sufficient to duplicate the stack-size
line command in the while
loop, but it was not.
When I run the script, the system puts the x
parameter as the last one in the order, and does not read it. I cannot understand what is going on.
Here's my code:
#!/bin/bash
#
# Example of how to parse short/long options with 'getopt'
#
OPTS=`getopt -o vhnxs: --long verbose,dry-run,help,xxx-size,stack-size: -n 'parse-options' -- "$@"`
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
echo "$OPTS"
eval set -- "$OPTS"
VERBOSE=false
HELP=false
DRY_RUN=false
STACK_SIZE=0
XXX=-1
printf "\n1st parameter: "$1"\n"
printf "2nd parameter: "$2"\n"
printf "3rd parameter: "$3"\n"
printf "4th parameter: "$4"\n"
printf "5th parameter: "$5"\n\n"
while true; do
case "$1" in
-v | --verbose ) VERBOSE=true; shift ;;
-h | --help ) HELP=true; shift ;;
-n | --dry-run ) DRY_RUN=true; shift ;;
-x | --xxx-size ) XXX="$2"; shift; shift ;;
-s | --stack-size ) STACK_SIZE="$2"; shift; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
echo VERBOSE=$VERBOSE
echo HELP=$HELP
echo DRY_RUN=$DRY_RUN
printf "STACK_SIZE "$STACK_SIZE"\n"
printf "XXX "$XXX"\n"
printf "\n\n"
Here's what happens if I try to set the s
parameter to 100 and the x
parameters to 65:
$ ./script.sh -s 100 -x 65
Standard output:
-s '100' -x -- '65'
1st parameter: -s
2nd parameter: 100
3rd parameter: -x
4th parameter: --
5th parameter: 65
VERBOSE=false
HELP=false
DRY_RUN=false
STACK_SIZE 100
XXX --
As you can see, the program does not associate the value 65 to XXX
, as I would like. How can I solve this problem?
Thanks!