Advertisement
bunam

Untitled

Oct 27th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. getopt --test > /dev/null
  4. if [[ $? -ne 4 ]]; then
  5. echo "I’m sorry, `getopt --test` failed in this environment."
  6. exit 1
  7. fi
  8.  
  9. OPTIONS=dfo:v
  10. LONGOPTIONS=debug,force,output:,verbose
  11.  
  12. # -temporarily store output to be able to check for errors
  13. # -e.g. use “--options” parameter by name to activate quoting/enhanced mode
  14. # -pass arguments only via -- "$@" to separate them correctly
  15. PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTIONS --name "$0" -- "$@")
  16. if [[ $? -ne 0 ]]; then
  17. # e.g. $? == 1
  18. # then getopt has complained about wrong arguments to stdout
  19. exit 2
  20. fi
  21. # read getopt’s output this way to handle the quoting right:
  22. eval set -- "$PARSED"
  23.  
  24. # now enjoy the options in order and nicely split until we see --
  25. while true; do
  26. case "$1" in
  27. -d|--debug)
  28. d=y
  29. shift
  30. ;;
  31. -f|--force)
  32. f=y
  33. shift
  34. ;;
  35. -v|--verbose)
  36. v=y
  37. shift
  38. ;;
  39. -o|--output)
  40. outFile="$2"
  41. shift 2
  42. ;;
  43. --)
  44. shift
  45. break
  46. ;;
  47. *)
  48. echo "Programming error"
  49. exit 3
  50. ;;
  51. esac
  52. done
  53.  
  54. # handle non-option arguments
  55. if [[ $# -ne 1 ]]; then
  56. echo "$0: A single input file is required."
  57. exit 4
  58. fi
  59.  
  60. echo "verbose: $v, force: $f, debug: $d, in: $1, out: $outFile"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement