1
0

getopts-template.sh 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/bash
  2. # Klimbim Software collection, A bag full of things
  3. # Copyright (C) 2011-2023 Johannes 'Banana' Keßler
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. # 2023 http://www.bananas-playground.net
  18. # this is a small example how to use getops in bash
  19. SELF=`basename "$0"`
  20. Help() {
  21. # Display Help
  22. echo "Syntax: ${SELF} [-n|h|v|V]"
  23. echo "Available ptions are:"
  24. echo "-n Input your name."
  25. echo "-h Print this Help."
  26. echo "-v Verbose mode."
  27. echo "-V Print software version and exit."
  28. echo
  29. }
  30. VERBOSE="false"
  31. NAME=""
  32. while getopts "hvVn:" option ; do
  33. case $option in
  34. h) Help
  35. exit;;
  36. v) VERBOSE="true" ;;
  37. V) echo "Version 1.0"
  38. exit;;
  39. n) NAME=$OPTARG ;;
  40. ?) # Invalid option
  41. Help
  42. exit;;
  43. esac
  44. done
  45. if [[ -z "$NAME" ]] ; then
  46. echo 'Missing -n' >&2
  47. exit 1
  48. fi
  49. echo "${NAME}"