-
Notifications
You must be signed in to change notification settings - Fork 0
/
existing.sh
executable file
·209 lines (169 loc) · 3.73 KB
/
existing.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
#!/bin/bash
#
# shell source working directory
#
wd=$(dirname $0)
#
# parameters
#
parameters="$*"
#
# documentation
#
function usage {
cat<<EOF>&2
Synopsis
$0
Description
Lookup existing filename with default filename extension 'tex'.
Filename extension
$0 [a-z][a-z][a-z]
Lookup existing filename with argument filename extension in
{prefix}-{date}-[{subtitle}-]{index}.{ext}.
Parameters
$0 %p[A-Za-z_0-9]+
Lookup existing filename with argument filename <prefix> in
{prefix}-{date}-[{subtitle}-]{index}.{ext}
$0 %d[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
Lookup existing filename with argument filename <date> in
{prefix}-{date}-[{subtitle}-]{index}.{ext}
$0 %s[A-Za-z_0-9]+
Lookup existing filename with argument filename <subtitle>
in
{prefix}-{date}-[{subtitle}-]{index}.{ext}
$0 %i[0-9]
Lookup existing filename with argument filename <index> in
{prefix}-{date}-[{subtitle}-]{index}.{ext}
EOF
}
for arg in ${parameters}
do
case ${arg} in
-h|-?)
usage
exit 1;;
esac
done
#
# parameter '%p[A-Za-z_0-9]' (prefix)
#
function init_p {
for arg in ${parameters}
do
case ${arg} in
%p*)
echo ${arg} | sed 's/%p//'
return 0;;
esac
done
if ${wd}/prefix.sh
then
return 0
else
return 1
fi
}
component_p=$(init_p)
#
# parameter '%d[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' (date)
#
function init_d {
for arg in ${parameters}
do
case ${arg} in
%d[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])
echo ${arg} | sed 's/%d//'
return 0;;
esac
done
p="${component_p}-"
if tgt=$(2>/dev/null ls ${p}* | sort -V | tail -n 1 ) &&[ -n "${tgt}" ]
then
if tdd=$(echo "${tgt}" | sed "s%${p}%%;" | sed 's%-[0-9]*\.[a-z][a-z][a-z]$%%; s%-[A-Za-z_]*%%;') &&[ -n "${tdd}" ]
then
echo ${tdd}
return 0
fi
fi
date +%Y%m%d
return 0
}
component_d=$(init_d)
#
# parameter '%i[0-9]' (index)
#
function init_i {
for arg in ${parameters}
do
case ${arg} in
%i[0-9]*)
echo ${arg} | sed 's/%i//'
return 0;;
esac
done
p="${component_p}-${component_d}-"
if tgt=$(2>/dev/null ls ${p}* | sort -V | tail -n 1 ) && [ -n "${tgt}" ]
then
if n=$(echo "${tgt}" | sed "s%${p}%%; s%\.[a-z][a-z][a-z]\$%%; s%[A-Za-z_]+-%%;") &&[ -n "${n}" ]
then
echo ${n}
return 0
fi
fi
echo 0
return 0
}
component_i=$(init_i)
#
# parameter '%x[a-z][a-z][a-z]' (fext)
#
function init_x {
#
for arg in ${parameters}
do
case ${arg} in
%x*)
echo ${arg} | sed 's/%x//'
return 0;;
esac
done
#
for arg in ${parameters}
do
case ${arg} in
[a-z][a-z][a-z])
echo ${arg}
return 0;;
esac
done
echo "tex"
return 0
}
component_x=$(init_x)
#
# optional component subtitle has parameter '%s[A-Za-z_0-9]'
#
function init_s {
for arg in ${parameters}
do
case ${arg} in
%s*)
echo ${arg} | sed 's/%s//'
return 0;;
esac
done
return 1
}
if component_s=$(init_s) &&[ -n "${component_s}" ]
then
file=${component_p}-${component_d}-${component_s}-${component_i}.${component_x}
else
file=${component_p}-${component_d}-${component_i}.${component_x}
fi
if [ -n "${file}" ]&&[ -f "${file}" ]
then
echo "${file}"
exit 0
else
exit 1
fi