summaryrefslogtreecommitdiffstats
path: root/bash_experiments/cmd_options.sh
blob: e1b5656597fa9ef6c305baf73ff890efe78ff4e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash

echo $0
echo $1

Help() {
   echo "Add description of the script functions here."
   echo
   echo "Syntax: scriptTemplate [-g|h|v|V]"
   echo "options:"
   echo "g     Print the GPL license notification."
   echo "h     Print this Help."
   echo "v     Verbose mode."
   echo "V     Print software version and exit."
   echo
}

Name=World

#
#  The while-done structure defines a loop that executes once for each option
# in the getopts - option structure. The ":h" string - which requires the quotes
# - lists the possible input options that will be evaluated by the case - esac
# structure. Each option listed must have a corresponding stanza in the case statement.
# exit;; exits from the program without executing any more code even if some
# exists. The option processing loop is also terminated, so no additional
# options would be checked
while getopts ":hn:" option; do

	case $option in
		h) # display Help
			Help
			exit;;
		n) # Enter a name
			Name=$OPTARG;;
		\?) # invalid option
			echo "Error: Invalid option"
			echo $option
			exit;;
	esac

done

echo "Hello $Name"