-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewscript
executable file
·175 lines (144 loc) · 2.47 KB
/
newscript
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
#!/bin/bash
script="$(basename "$0")"
basic=false
openEditor=false
editor=idea
usage() {
cat <<-DOC
Usage: $script [-beh] [file]
Creates a new shell template. Named tmp#.
You might want a full or basic template. A full template has a flags mechanism.
Default is "full".
Examples:
$script
$script -b -e
$script -e tea.sh
Options:
-b, --basic create basic template
-e, --editor open in txt editor
-h, --help show this help message
DOC
}
template=$(cat <<\EOF
#!/bin/bash
usage() {
script="./$(basename "$0")"
cat <<-DOC
Usage: $script {-s|-t} PATTERN
Summary, help, etc.
Options:
-s, --slong slong explanation
-t, --tlong tlong explanation
-h, --help show this help message
DOC
}
if [ $# -eq 0 ]; then
usage
exit
fi
if [ $# -lt 2 ]; then
echo "Try '$script -h' for more information."
exit 1
fi
while [ $# -gt 0 ]; do
case $1 in
-s | --slong)
# code here
;;
-t | --tlong)
# code here
;;
-h | --help)
usage
exit
;;
*)
# code here
;;
esac
shift
done
EOF
)
createBasic() {
local file=$1
printf "#!/bin/bash\n\n" > "$file"
chmod +x "$file"
echo "Created a new basic script: $file"
}
createFull() {
local file=$1
echo "$template" > "$file"
chmod +x "$file"
echo "Created a new full script: $file"
}
requireBinary() {
local binary line
binary=$1
if ! command -v "$binary" &> /dev/null; then
line=$(caller | awk '{print $1}')
echo "Error: $binary is not installed. Check line $line."
return 1
fi
return 0
}
inPath() {
local file err
file=$1
err="Error: $file already exists in PATH."
if [[ $SHELL =~ zsh ]]; then
zsh -c "source ~/.zshrc; command -v $file" &> /dev/null
exitCode=$?
if [[ $exitCode == 0 ]]; then
echo "$err"
return 0
fi
fi
if command -v "$file" &> /dev/null; then
echo "$err"
return 0
fi
return 1
}
new() {
local file=$1
if $basic; then
createBasic "$file"
else
createFull "$file"
fi
}
while [ $# -gt 0 ]; do
case $1 in
-b | --basic)
basic=true
;;
-e | --editor)
requireBinary "$editor" || exit $?
openEditor=true
;;
-h | --help)
usage
exit
;;
*)
file=$1
esac
shift
done
inPath "$file" && exit
if [[ -n $file ]]; then
new "$file"
$openEditor && "$editor" "$file" &> /dev/null & disown
exit
fi
cnt=1
while true; do
file="tmp${cnt}"
if [[ ! -f "$file" ]]; then
new "$file"
$openEditor && "$editor" "$file" &> /dev/null & disown
break
fi
((cnt++))
done