-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash_option_parsing
More file actions
executable file
·68 lines (59 loc) · 1.8 KB
/
bash_option_parsing
File metadata and controls
executable file
·68 lines (59 loc) · 1.8 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
## Options Parsing ##
function parseOptions(){
invalidOptionFlag=0 # set invalid option boolean
options=($@) # put the options into an array
while [ ${#options[@]} -gt 0 ]; do # while there are options to parse
case ${options} in # Examin the current option
(--*) # If it is a long option
case ${options} in # Parse the long option
(--outfile)
options=(${options[@]#${options}})
if [ "${options:0:1}" == "-" ]; then
continue
else
outFile="${options}"
options=(${options[@]#${options}})
continue
fi
;;
(*)
echo "invalid option ${options}"
invalidOptionFlag=1
;;
esac
;;
(-*) # If it is a short option
if [ ${#options} -lt 3 ]; then # If it is a single short option
case ${options} in
(-y)
testFlag=1
;;
(*)
echo "invalid option ${options}"
invalidOptionFlag=1
;;
esac
else # Create single short options from combined short option
multiOption="${options}"
first="-${multiOption:1:1}"
remaining="-${multiOption:2}"
options=(${options[@]#${options}} $first $remaining)
continue
fi
;;
esac
options=(${options[@]#${options}}) # Remove the parsed option from options array
done
return $invalidOptionFlag
}
## Set variables ##
testFlag=0
outFile=''
## Parse options ##
parseOptions $@
## Main ##
if [ $? == 0 ]; then
echo "testFlag = $testFlag"
echo "outFile = $outFile"
fi