-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripstrap
executable file
·127 lines (104 loc) · 1.92 KB
/
scripstrap
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
#!/bin/bash
#Script for starting new scripts
name=""
overwrite=false
args=0
opts=""
usage (){
echo "${0##*/} - Generates a bash script stub."
echo "Usage: $(basename $0) name [nrOfargs] [opts]"
exit 1
}
# Parse options
#=============================
while getopts "oh" opt; do
case $opt in
h )
usage
;;
o )
overwrite=true
;;
? )
usage
;;
esac
done
shift $((OPTIND -1))
# Parse arguments
#===============================
if [ "$#" -lt 1 ]; then
usage
fi
echo -n "Generating script $name"
if [ ! -z "$2" ]; then
args=$2
echo " with $args arguments "
fi
if [ ! -z "$3" ]; then
opts="$3"
echo "Opts: $opts"
fi
name=$1
if [ -f $1 ]; then
if [ "$overwrite" = false ]; then
echo "Error: File exists!"
exit
fi
fi
# Begin output
#=================================
#Brackets redirect all output to target file
{
touch $name
chmod +x $name
echo "#!/bin/bash
#Autogenerated script template
"
#Create argument declarations
echo "# Variable declarations
#========================================
"
echo "me=\${0##*/}"
for (( i=0; i<$args; i++ )); do
echo "_arg${i}_=\"\""
done
#Generate usage function
echo '
usage (){
echo "$me - Description missing"
echo "Usage: $me"
exit 1
}'
#Generate opts parsing
echo "
# Parse options
#=======================================
while getopts \"h$opts\" opt; do"
echo ' case $opt in'
for (( i=0; i<${#opts}; i++ )); do
echo " ${opts:$i:1})
#dostuff
;;"
done
echo ' h )
usage
;;
\? )
usage
;;
: )
echo "Option requires an argument: $OPTARG"
exit
;;
esac
done
shift $((OPTIND -1))
'
echo -n 'if [ "$#" -lt '
echo "$args ]; then
usage
fi
"
}> $name
$EDITOR $name