Repository navigation
Expand file tree
/
Copy pathmake_bash_template.sh
More file actions
executable file
·166 lines (150 loc) · 4.94 KB
/
Copy pathmake_bash_template.sh
File metadata and controls
executable file
·166 lines (150 loc) · 4.94 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
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
#!/bin/bash
# Copyright Mike Rightmire, Biocom Software
# mike.rightmire@biocomsoftware.com
### USAGE ######################################################################
#
#make_bash_template.sh takes in a list of parameters, andcreates a file with all the annoying
# busywork of creating help fields, usage statements and the getopts.
#
# Example:
# make_bash_template.sh -n MyScript -p infile -p outfile -p param
#
# -n |
# The name of the output file. ".sh" will be added automagically.
# (MANDATORY)
#
# -p |
# Description.
# (DEFAULT: )
#
### USAGE ######################################################################
while getopts "n:p:" opt; do
case $opt in
n) name+=("$OPTARG");;
p) longs+=("$OPTARG");;
esac
done
shift $((OPTIND -1))
declare -a shorts
# Pipe all echo also to file
exec 6>&1 # Link file descriptor #6 with stdout.
exec > ./${name}.sh
### Make the usage statement
# Using multiple echos instead of "cat <<EOT >> ./${name}.sh"
# because of buffer issues (text being written to file out of order)
echo "#!/bin/bash"
echo "### USAGE ######################################################################"
echo "read -r -d '' USAGE << ENDOFUSAGE"
echo ""
echo "${name}.sh uses iptables to block the NFS ports from the hostnames listed in the configuration file. "
echo "This script actively writes the iptables. It does NOT save them (to persist past a reboot.) This is intentional to avoid accidental administrative lock-outs. "
echo ""
echo " -h | --help "
echo " Print this help statement."
count=0
for val in "${longs[@]}"; do
shorts[$count]=${val:0:1}
echo ""
echo " -${shorts[$count]} | --$val"
echo " Description."
echo " (DEFAULT: )"
((count++))
done
echo ""
echo " -d | --dry-run"
echo " Take no real action."
echo " (DEFAULT: false)"
echo ""
echo " -v | --verbose"
echo " Verbose output."
echo " (DEFAULT: false)"
echo ""
echo "ENDOFUSAGE"
echo "### USAGE ######################################################################"
### Set the getops stuff
# Final command like...
# OPTS=`getopt -o hc:dv --longoptions help,config-file,dry-run,verbose: -n 'parse-options' -- "$@"`
echo "#--- SET GETOPTS --------------------------------------------------------------"
echo -n "OPTS=\`getopt -o h" # Set initial
#echo $command > ./${name}.sh
# Do the short (single character) params first
for short in "${shorts[@]}"; do
echo -n "${short}:"
done
echo -n "dv"
# Do the long versions next
echo -n " --longoptions help,"
for long in "${longs[@]}"; do
echo -n "${long},"
done
# Delete the final trailing comma
#echo -en '\b'
# Finish the command
echo "dry-run,verbose: -n 'parse-options' -- \"\$@\"\`"
### The written script's check that the options were parsed
echo ""
echo "if [ $? != 0 ] ; then"
echo " echo \"Failed parsing options.\" >&2"
echo " echo \"$USAGE\" >&2"
echo " exit 1"
echo "fi"
echo "# echo \"\$OPTS\""
echo "eval set -- \"\$OPTS\""
echo ""
### Written script's Set the defaults
echo "#--- SET DEFAULTS --------------------------------------------------------------"
for long in "${longs[@]}"; do
long=${long//-}
# Remove any dashes, but leave underscores
echo "${long^^}=\"\""
done
echo "DRY_RUN=false"
echo "VERBOSE=false"
### Written script's run the getopts
echo ""
echo "#--- RUN GETOPTS --------------------------------------------------------------"
echo "while true; do"
echo " case \"\$1\" in"
echo " -h | --help) echo \"\$USAGE\" >&2; shift ;;"
count=0
for val in "${longs[@]}"; do
shorts[$count]=${val:0:1}
variable=${val//-}
echo " -${shorts[$count]} | --${val}) ${variable^^}=\"\$2\"; shift; shift ;;"
((count++))
done
echo " -d | --dry-run) DRY_RUN=true; shift ;;"
echo " -v | --verbose) VERBOSE=true; shift ;;"
echo " -- ) shift; break ;;"
echo " * ) break ;;"
echo " esac"
echo "done"
### Written script's Print verbose if set
echo ""
echo "if [ \${VERBOSE} == true ]; then"
for long in "${longs[@]}"; do
long=${long//-}
echo " echo \"${long^^} = \$${long^^}\""
done
echo "fi"
## Written script's check for parameters
echo ""
echo "#--- PARAM MODS --------------------------------------------------------------"
echo "# [[ ! -f \${CONFIGFILE} ]] && { echo; echo \"Config file \${CONFIGFILE} does not appear to exist or is not readable. \"; exit 1; }"
### Written script's function placeholder
echo ""
echo "#--- FUNCTIONS --------------------------------------------------------------"
echo "function command() {"
echo " if [ \${DRY_RUN} == true ] || [ \${VERBOSE} == true ]; then"
echo " echo \"\$1\""
echo " fi"
echo " if [ \${DRY_RUN} != true ]; then"
echo " eval \"\$1\""
echo " fi"
echo "}"
### Written script's placehold for the main function
echo ""
echo "# MAIN() ---------------------------------------------------------------------"
echo "command \"ls -la\""
### Make the new script executable
chmod 700 ./${name}.sh