-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakebashscript.sh
executable file
·300 lines (259 loc) · 6.93 KB
/
makebashscript.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/bin/bash -e
#################################################################
#** Version 1.0
#* This script helps creating bash scripts nicely and uniformly
#* while saving time..
#################################################################
# author/copyright: michael.lustenberger at inofix.ch
# license: gladly sharing with the universe and any lifeform,
# be it naturally born or constructed, alien or kin..
# USE AT YOUR OWN RISK.
#################################################################
[ "$1" == "debug" ] && shift && set -x
## variables ##
### you may copy the following variables into this file for having your own
### local config ...
conffile=~/.makebashscript
### {{{
author_name=""
author_email=""
### }}}
### ... now, leave alone from here on.
scriptname="$1"
license="$2"
## functions ##
print_usage()
{
echo "usage: $0 scriptname [ license ]"
}
die()
{
echo "$1"
exit 1
}
error()
{
print_usage
echo ""
die "Error: $@"
}
ask_to_write_config()
{
echo -n "Do you want to permanently store this in $conffile? [N|y] "
read answer
case "$answer" in
Y*|y*)
echo "author_name='$author_name'" > $conffile
echo "author_email='$author_email'" >> $conffile
;;
esac
}
ask_author()
{
echo "Please provide some author information for your script:"
echo -n "Your name: "
read author_name
echo -n "Your email: "
read author_email
ask_to_write_config
}
## logic ##
case $scriptname in
-*)
print_usage
/usr/bin/head $0 | /bin/grep "#\*"
exit 0
;;
"")
error "Please provide a name for your script."
;;
esac
[ -r "$conffile" ] && . $conffile
gitconf=~/.gitconfig
if [ -r "$gitconf" ] ; then
git_name=$(grep "name = " $gitconf | awk '{print $3" "$4}')
git_email=$(grep "email = " $gitconf | awk '{print $3}')
# if both differ, it is probably badly configured..
if [ -n "$git_name" ] && [ "$git_name" != "$author_name" ] &&
[ -n "$git_email" ] && [ "$git_name" != "$author_name" ] ; then
echo "Do you want to use the git data for your personal info?"
echo "Your name: $git_name"
echo "Your email: <$git_email>"
echo -n "Change? [N|y] "
read answer
case "$answer" in
Y*|y*)
author_name="$git_name"
author_email="<$git_email>"
ask_to_write_config
;;
*)
ask_author
;;
esac
fi
elif [ -z "$author_name" ] ; then
ask_author
fi
case "$license" in
gpl*)
license=\
"########################################################################
#
# This is Free Software; feel free to redistribute and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# Copyright $(/bin/date '+%Y'), $author_name $author_email
#
########################################################################"
;;
*) license=\
"########################################################################
# author/copyright: $author_email
# license: gladly sharing with the universe and any lifeform,
# be it naturally born or constructed, alien or kin..
# USE AT YOUR OWN RISK.
########################################################################"
;;
esac
if [ -e "$scriptname" ] ; then
error "A script with this name does already exist! NOT overwriteing.."
fi
/bin/cat > $scriptname << EOF
#!/bin/bash -e
########################################################################
#** Version:
#* This script helps ..?
#
# note: the frame for this script was auto-created with
# *https://github.com/inofix/admin-toolbox/blob/master/makebashscript.sh*
$license
[ "\$1" == "debug" ] && shift && set -x
## variables ##
### you may copy the following variables into this file for having your own
### local config ...
#conffile=.$scriptname
### {{{
dryrun=1
needsroot=1
### }}}
# Unsetting this helper variable
_pre=""
# The system tools we gladly use. Thank you!
declare -A sys_tools
sys_tools=( ["_awk"]="/usr/bin/awk"
["_cat"]="/bin/cat"
["_cp"]="/bin/cp"
["_grep"]="/bin/grep"
["_id"]="/usr/bin/id"
["_mkdir"]="/bin/mkdir"
["_pwd"]="/bin/pwd"
["_rm"]="/bin/rm"
["_rmdir"]="/bin/rmdir"
["_sed"]="/bin/sed"
["_sed_forced"]="/bin/sed"
["_tr"]="/usr/bin/tr" )
# this tools get disabled in dry-run and sudo-ed for needsroot
danger_tools=( "_cp" "_cat" "_dd" "_mkdir" "_sed" "_rm" "_rmdir" )
# special case sudo (not mandatory)
_sudo="/usr/bin/sudo"
## functions ##
print_usage()
{
echo "usage: \$0"
}
print_help()
{
print_usage
\$_grep "^#\* " \$0 | \$_sed_forced 's;^#\*;;'
}
print_version()
{
\$_grep "^#\*\* " \$0 | \$_sed 's;^#\*\*;;'
}
die()
{
echo "\$@"
exit 1
}
error()
{
print_usage
echo ""
die "Error: \$@"
}
## logic ##
## first set the system tools
for t in \${!sys_tools[@]} ; do
if [ -x "\${sys_tools[\$t]##* }" ] ; then
export \${t}="\${sys_tools[\$t]}"
else
error "Missing system tool: \${sys_tools[\$t]##* } must be installed."
fi
done
[ ! -f "/etc/\$conffile" ] || . "/etc/\$conffile"
[ ! -f "/usr/etc/\$conffile" ] || . "/usr/etc/\$conffile"
[ ! -f "/usr/local/etc/\$conffile" ] || . "/usr/local/etc/\$conffile"
[ ! -f ~/"\$conffile" ] || . ~/"\$conffile"
[ ! -f "\$conffile" ] || . "\$conffile"
#* options:
while true ; do
case "\$1" in
#* -c |--config conffile alternative config file
-c|--config)
shift
if [ -r "\$1" ] ; then
. \$1
else
die " config file \$1 does not exist."
fi
;;
#* -h |--help print this help
-h|--help)
print_help
exit 0
;;
#* -n |--dry-run do not change anything
-n|--dry-run)
dryrun=0
;;
#* -v |--version
-v|--version)
print_version
exit
;;
-*|--*)
error "option \$1 not supported"
;;
*)
break
;;
esac
shift
done
if [ \$dryrun -eq 0 ] ; then
_pre="echo "
fi
if [ \$needsroot -eq 0 ] ; then
iam=\$(\$_id -u)
if [ \$iam -ne 0 ] ; then
if [ -x "\$_sudo" ] ; then
_pre="\$_pre \$_sudo"
else
error "Priviledges missing: use \${_sudo}."
fi
fi
fi
for t in \${danger_tools[@]} ; do
export \${t}="\$_pre \${sys_tools[\$t]}"
done
exit 0
EOF
chmod 755 $scriptname
vi $scriptname