-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellScriptTemplate.sh
More file actions
executable file
·79 lines (69 loc) · 1.6 KB
/
Copy pathshellScriptTemplate.sh
File metadata and controls
executable file
·79 lines (69 loc) · 1.6 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
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env bash
# A small, safe Bash script starter. Replace the description and main body.
set -Eeuo pipefail
usage() {
cat <<'USAGE'
Usage: script-name [-h] [-v] [-f] -p VALUE ARG [ARG...]
Describe what the script does.
Options:
-h, --help Show this help and exit
-v, --verbose Enable shell tracing
-f, --flag Example boolean flag
-p, --param VALUE Example required parameter
USAGE
}
cleanup() {
# Remove temporary resources here.
:
}
die() {
local message="$1"
local code="${2:-1}"
printf 'error: %s\n' "$message" >&2
exit "$code"
}
main() {
local flag=false
local param=""
local -a args=()
while [ "$#" -gt 0 ]; do
case "$1" in
-h | --help)
usage
return 0
;;
-v | --verbose)
set -x
shift
;;
-f | --flag)
flag=true
shift
;;
-p | --param)
[ "$#" -ge 2 ] || die "$1 requires a value"
param="$2"
shift 2
;;
--)
shift
args+=("$@")
break
;;
-*) die "unknown option: $1" ;;
*)
args+=("$1")
shift
;;
esac
done
[ -n "$param" ] || die "missing required option: --param"
[ "${#args[@]}" -gt 0 ] || die "at least one positional argument is required"
printf 'flag=%s\n' "$flag"
printf 'param=%s\n' "$param"
printf 'arguments=%s\n' "${args[*]}"
}
trap cleanup EXIT
trap 'exit 130' SIGINT
trap 'exit 143' SIGTERM
main "$@"