| 1 | #!/bin/bash |
| 2 | # UseGetOpt.sh |
| 3 | |
| 4 | # Author: Peggy Russell <[email protected]> |
| 5 | |
| 6 | UseGetOpt () { |
| 7 | declare inputOptions |
| 8 | declare -r E_OPTERR=85 |
| 9 | declare -r ScriptName=${0##*/} |
| 10 | declare -r ShortOpts="adf:hlt" |
| 11 | declare -r LongOpts="aoption,debug,file:,help,log,test" |
| 12 | |
| 13 | DoSomething () { |
| 14 | echo "The function name is '${FUNCNAME}'" |
| 15 | # Recall that $FUNCNAME is an internal variable |
| 16 | #+ holding the name of the function it is in. |
| 17 | } |
| 18 | |
| 19 | inputOptions=$(getopt -o "${ShortOpts}" --long \ |
| 20 | "${LongOpts}" --name "${ScriptName}" -- "${@}") |
| 21 | |
| 22 | if [[ ($? -ne 0) || ($# -eq 0) ]]; then |
| 23 | echo "Usage: ${ScriptName} [-dhlt] {OPTION...}" |
| 24 | exit $E_OPTERR |
| 25 | fi |
| 26 | |
| 27 | eval set -- "${inputOptions}" |
| 28 | |
| 29 | # Only for educational purposes. Can be removed. |
| 30 | #----------------------------------------------- |
| 31 | echo "++ Test: Number of arguments: [$#]" |
| 32 | echo '++ Test: Looping through "$@"' |
| 33 | for a in "$@"; do |
| 34 | echo " ++ [$a]" |
| 35 | done |
| 36 | #----------------------------------------------- |
| 37 | |
| 38 | while true; do |
| 39 | case "${1}" in |
| 40 | --aoption | -a) # Argument found. |
| 41 | echo "Option [$1]" |
| 42 | ;; |
| 43 | |
| 44 | --debug | -d) # Enable informational messages. |
| 45 | echo "Option [$1] Debugging enabled" |
| 46 | ;; |
| 47 | |
| 48 | --file | -f) # Check for optional argument. |
| 49 | case "$2" in #+ Double colon is optional argument. |
| 50 | "") # Not there. |
| 51 | echo "Option [$1] Use default" |
| 52 | shift |
| 53 | ;; |
| 54 | |
| 55 | *) # Got it |
| 56 | echo "Option [$1] Using input [$2]" |
| 57 | shift |
| 58 | ;; |
| 59 | |
| 60 | esac |
| 61 | DoSomething |
| 62 | ;; |
| 63 | |
| 64 | --log | -l) # Enable Logging. |
| 65 | echo "Option [$1] Logging enabled" |
| 66 | ;; |
| 67 | |
| 68 | --test | -t) # Enable testing. |
| 69 | echo "Option [$1] Testing enabled" |
| 70 | ;; |
| 71 | |
| 72 | --help | -h) |
| 73 | echo "Option [$1] Display help" |
| 74 | break |
| 75 | ;; |
| 76 | |
| 77 | --) # Done! $# is argument number for "--", $@ is "--" |
| 78 | echo "Option [$1] Dash Dash" |
| 79 | break |
| 80 | ;; |
| 81 | |
| 82 | *) |
| 83 | echo "Major internal error!" |
| 84 | exit 8 |
| 85 | ;; |
| 86 | |
| 87 | esac |
| 88 | echo "Number of arguments: [$#]" |
| 89 | shift |
| 90 | done |
| 91 | |
| 92 | shift |
| 93 | # Only for educational purposes. Can be removed. |
| 94 | #---------------------------------------------------------------------- |
| 95 | echo "++ Test: Number of arguments after \"--\" is [$#] They are: [$@]" |
| 96 | echo '++ Test: Looping through "$@"' |
| 97 | for a in "$@"; do |
| 98 | echo " ++ [$a]" |
| 99 | done |
| 100 | #---------------------------------------------------------------------- |
| 101 | |
| 102 | } |
| 103 | |
| 104 | ################################### M A I N ######################## |
| 105 | # If you remove "function UseGetOpt () {" and corresponding "}", |
| 106 | #+ you can uncomment the "exit 0" line below, and invoke this script |
| 107 | #+ with the various options from the command-line. |
| 108 | #------------------------------------------------------------------- |
| 109 | # exit 0 |
| 110 | |
| 111 | echo "Test 1" |
| 112 | UseGetOpt -f myfile one "two three" four |
| 113 | |
| 114 | echo;echo "Test 2" |
| 115 | UseGetOpt -h |
| 116 | |
| 117 | echo;echo "Test 3 - Short Options" |
| 118 | UseGetOpt -adltf myfile anotherfile |
| 119 | |
| 120 | echo;echo "Test 4 - Long Options" |
| 121 | UseGetOpt --aoption --debug --log --test --file myfile anotherfile |
| 122 | |
| 123 | exit |