bashGetopt.sh 904 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/bin/bash
  2. # SOURCE: https://gist.github.com/cosimo/3760587
  3. # SEE ALSO: http://stackoverflow.com/questions/15103482/bash-getopt-command-returns-its-own-parameters-instead-of-command-line-parameter
  4. #
  5. # Example of how to parse short/long options with 'getopt'
  6. #
  7. # getopt links:
  8. #
  9. OPTS=`getopt -o vhns: --long verbose,dry-run,help,stack-size: -n 'parse-options' -- "$@"`
  10. if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
  11. echo "$OPTS"
  12. eval set -- "$OPTS"
  13. VERBOSE=false
  14. HELP=false
  15. DRY_RUN=false
  16. STACK_SIZE=0
  17. while true; do
  18. case "$1" in
  19. -v | --verbose ) VERBOSE=true; shift ;;
  20. -h | --help ) HELP=true; shift ;;
  21. -n | --dry-run ) DRY_RUN=true; shift ;;
  22. -s | --stack-size ) STACK_SIZE="$2"; shift; shift ;;
  23. -- ) shift; break ;;
  24. * ) break ;;
  25. esac
  26. done
  27. echo VERBOSE=$VERBOSE
  28. echo HELP=$HELP
  29. echo DRY_RUN=$DRY_RUN
  30. echo STACK_SIZE=$STACK_SIZE