Debian Codegraph @

UseGetOpt.sh

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2 # UseGetOpt.sh
3
4 # Author: Peggy Russell <[email protected]>
5
6 UseGetOptscip-shell shell abs-guide 10-4 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 DoSomethingscip-shell shell abs-guide 10-4 DoSomething(). () {
14 echoscip-shell system . . 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=$(getoptscip-shell system . . getopt. -o "${ShortOpts}" --long \
20 "${LongOpts}" --name "${ScriptName}" -- "${@}")
21
22 if [[ ($? -ne 0) || ($# -eq 0) ]]; then
23 echoscip-shell system . . 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 echoscip-shell system . . echo. "++ Test: Number of arguments: [$#]"
32 echoscip-shell system . . echo. '++ Test: Looping through "$@"'
33 for a in "$@"; do
34 echoscip-shell system . . echo. " ++ [$a]"
35 done
36 #-----------------------------------------------
37
38 while truescip-shell system . . true.; do
39 case "${1}" in
40 --aoption | -a) # Argument found.
41 echoscip-shell system . . echo. "Option [$1]"
42 ;;
43
44 --debug | -d) # Enable informational messages.
45 echoscip-shell system . . 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 echoscip-shell system . . echo. "Option [$1] Use default"
52 shift
53 ;;
54
55 *) # Got it
56 echoscip-shell system . . echo. "Option [$1] Using input [$2]"
57 shift
58 ;;
59
60 esac
61 DoSomethingscip-shell shell abs-guide 10-4 DoSomething().
62 ;;
63
64 --log | -l) # Enable Logging.
65 echoscip-shell system . . echo. "Option [$1] Logging enabled"
66 ;;
67
68 --test | -t) # Enable testing.
69 echoscip-shell system . . echo. "Option [$1] Testing enabled"
70 ;;
71
72 --help | -h)
73 echoscip-shell system . . echo. "Option [$1] Display help"
74 break
75 ;;
76
77 --) # Done! $# is argument number for "--", $@ is "--"
78 echoscip-shell system . . echo. "Option [$1] Dash Dash"
79 break
80 ;;
81
82 *)
83 echoscip-shell system . . echo. "Major internal error!"
84 exit 8
85 ;;
86
87 esac
88 echoscip-shell system . . echo. "Number of arguments: [$#]"
89 shift
90 done
91
92 shift
93 # Only for educational purposes. Can be removed.
94 #----------------------------------------------------------------------
95 echoscip-shell system . . echo. "++ Test: Number of arguments after \"--\" is [$#] They are: [$@]"
96 echoscip-shell system . . echo. '++ Test: Looping through "$@"'
97 for a in "$@"; do
98 echoscip-shell system . . 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 echoscip-shell system . . echo. "Test 1"
112 UseGetOptscip-shell shell abs-guide 10-4 UseGetOpt(). -f myfile one "two three" four
113
114 echoscip-shell system . . echo.;echoscip-shell system . . echo. "Test 2"
115 UseGetOptscip-shell shell abs-guide 10-4 UseGetOpt(). -h
116
117 echoscip-shell system . . echo.;echoscip-shell system . . echo. "Test 3 - Short Options"
118 UseGetOptscip-shell shell abs-guide 10-4 UseGetOpt(). -adltf myfile anotherfile
119
120 echoscip-shell system . . echo.;echoscip-shell system . . echo. "Test 4 - Long Options"
121 UseGetOptscip-shell shell abs-guide 10-4 UseGetOpt(). --aoption --debug --log --test --file myfile anotherfile
122
123 exit