-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbash-scaffold.sh
More file actions
119 lines (102 loc) · 2.69 KB
/
bash-scaffold.sh
File metadata and controls
119 lines (102 loc) · 2.69 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
#------------------------------------------------------------------------------------
# What: A quick-start BASH template script
# Author: Adam Hayward <adam at happy dot cat>
# License: http://svn.happy.cat/public/LICENSE.txt
#
# Features:
# log "A log message"
# debug "A debug message only displayed if '-v' option passed"
# error "A fatal error message and exit with code:" 2
# usage - print a usage message
# get_options - parse command-line options, defaults below
#
# Standard command-line options:
# -v verbose
# -h help message
# -V version number
# Long-format options:
# --version
# --help
#
#------------------------------------------------------------------------------------
#
# example-script.sh - Short description
#
# author: Author of script
# contact: Email
# since: Date
#
#------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------
# SCRIPT CONFIGURATION
#------------------------------------------------------------------------------------
SCRIPT_NAME=`basename $0`
# If debug information should be shown
VERBOSE=
VERSION=0.1
# Add your own global variables here
OP1=
OP2=
#------------------------------------------------------------------------------------
# UTILITY FUNCTIONS
#------------------------------------------------------------------------------------
# print a log a message
log ()
{
echo "[${SCRIPT_NAME}]: $1" > /dev/stderr
}
# print a debug message - only outputs is VERBOSE flag is set
debug()
{
[ "$VERBOSE" ] && echo "[${SCRIPT_NAME}]: $1" > /dev/stderr
}
# print an error message and exit()
error()
{
echo "[${SCRIPT_NAME}] ERROR: $1" > /dev/stderr
[ $# -gt 1 ] && exit $2
exit 1
}
# Define your own script functions here
# Print a usage message
usage()
{
cat << USAGE
usage: $0 [-v] [-h] -a op1 -b op2
Short description
REQUIRED OPTIONS:
-a op1 Option 1
-b op2 Option 2
OTHER OPTIONS:
-v Show debuging messages
-h Show this help message
-V Show version
USAGE
}
# Get the script options
get_options()
{
while getopts "a:b:hvV-:" OPTION
do
if [ $OPTION == "-" ]; then
OPTION=$OPTARG
fi
case $OPTION in
a) OP1=${OPTARG};;
b) OP2=${OPTARG};;
h) usage && exit 0;;
'help') usage && exit 0;;
V) echo $VERSION && exit 0;;
'version') echo $VERSION && exit 0;;
v) VERBOSE=1;;
\?) echo "Invalid option" && usage && exit 1;;
esac
done
}
main()
{
get_options "$@"
# Put the rest of your main script here
}
main "$@"