0

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!

DavideChicco.it
  • 3,318
  • 13
  • 56
  • 84
  • 2
    You passed `-s '100' -x -- '65'` instead of `-s '100' -x '65'`. Because `--` is the 4th parameter (and the parameter after -x), it assigns `--` to `XXX`. Then it reads 65 as next parameter and does not know how to handle it, thus breaking the case statement and outputting your variables. – Lukas Isselbächer Jul 18 '17 at 15:04
  • @LukasIsselbächer I did not pass `--` as input parameter. I don't know why the script thinks so! – DavideChicco.it Jul 18 '17 at 15:06
  • @DaviceChicco.it Please delete `eval set -- "$OPTS"` – Lukas Isselbächer Jul 18 '17 at 15:13
  • Then it will work :) – Lukas Isselbächer Jul 18 '17 at 15:22
  • @LukasIsselbächer Thanks! If you have time to re-write it as a proper answer, I can mark it as "accepted answer". – DavideChicco.it Jul 18 '17 at 15:26
  • @LukasIsselbächer Another little issue: I noticed that the program generates an error if I use `set -o nounset`, because it says `$1: unbound variable` for the `while` loop. I don't like that `while true`, and I think I should replace it with something else more reasonable. Any idea? – DavideChicco.it Jul 18 '17 at 15:56
  • You could do `for var in "$@" do ... done` – Lukas Isselbächer Jul 18 '17 at 16:12
  • The error it generates by using `set -o nounset` is because you are using $5 at the beginning in your printf, but it is unset. – Lukas Isselbächer Jul 18 '17 at 16:24
  • @LukasIsselbächer Thanks! Last thing: I would like to add a new parameter which is mandatory and has no flag (e.g. no `-x` flag). What special character should I put in the `while` loop to recognize it? – DavideChicco.it Jul 18 '17 at 16:24

0 Answers0