-
Notifications
You must be signed in to change notification settings - Fork 3
/
bash-arg-passing-1.sh
executable file
·60 lines (55 loc) · 1.2 KB
/
bash-arg-passing-1.sh
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
#!/usr/bin/env bash
# Set some default values:
ALPHA=a
BETA=b
CHARLIE=c
DELTA=d
usage() {
echo "Usage: alphabet [ -a | --alpha ] [ -b | --beta ]
[ -c | --charlie CHARLIE ]
[ -d | --delta DELTA ] filename(s)"
exit 2
}
PARSED_ARGUMENTS=$(getopt -a -n alphabet -o abc:d: --long alpha,bravo,charlie:,delta: -- "$@")
VALID_ARGUMENTS=$?
if [ "$VALID_ARGUMENTS" != "0" ]; then
usage
fi
echo "PARSED_ARGUMENTS is $PARSED_ARGUMENTS"
eval set -- "$PARSED_ARGUMENTS"
while :; do
case "$1" in
-a | --alpha)
ALPHA=1
shift
;;
-b | --beta)
BETA=1
shift
;;
-c | --charlie)
CHARLIE="$2"
shift 2
;;
-d | --delta)
DELTA="$2"
shift 2
;;
# -- means the end of the arguments; drop this, and break out of the while loop
--)
shift
break
;;
# If invalid options were passed, then getopt should have reported an error,
# which we checked as VALID_ARGUMENTS when getopt was called...
*)
echo "Unexpected option: $1 - this should not happen."
usage
;;
esac
done
echo "ALPHA : $ALPHA"
echo "BETA : $BETA "
echo "CHARLIE : $CHARLIE"
echo "DELTA : $DELTA"
echo "Parameters remaining are: $*"