forked from ZedThree/super-bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkgs2run
executable file
·361 lines (319 loc) · 8.48 KB
/
mkgs2run
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/bin/bash
#A script to create a gs2 run directory
#Author: David Dickinson
################################################
#Settings and default definitions
################################################
#//Executable
EXE_DEFAULT=~/bin/gs2
#//Directory name
PATH_DEF_PRE=GS2RUN
#//Executable target name
TARG_EXE_DEF="gs2"
#//Variable replacement token
VARTOKDEF="VAR_REPLACE_"
################################################
#Load in other functions
################################################
source ~/bin/pickfile
source ~/bin/errmesg
source ~/bin/warnmesg
source ~/bin/yesno
source ~/bin/rel2abs
################################################
#Options and usage
################################################
usage()
{
cat <<EOF
usage: $0 Options
This script makes an execution directory (defaults are gs2 specific).
OPTIONS:
-h Show this message
-e <VAL> Path to executable to use. Defaults to ${EXE_DEFAULT}
-E <VAL> Executable target name. Defaults to gs2.
-d <VAL> Directory name/path+name. Defaults to ${PATH_DEF_PRE}_<DATE+TIME>
-i <VAL> Input file to use. If not set then will search current directory for *.in files
-I <VAL> Name of input file target (i.e. what it will be called when it's copied, defaults to input file name)
-V <VAL> Variable replacement token. Defaults to VAR_REPLACE_ . Note assumes tokens are numbered
sequentially starting at 1 (e.g. VAR_REPLACE_1, VAR_REPLACE_2 etc.)
-v <VAL> Comma separated list of variables to use as replacement values.
-s <VAL> Submission script to copy into directory.
-S <VAL> If set then calls mksub in directory. Set to a quoted string containing options to pass to mksub.
Note: Doesn't clean input so it's possible to chain additional commands on the end (by appending ; cmd ..)
to then end of the options.
-t Dryrun mode, only print information don't actually do anything.
-a Append date to directory
EOF
}
################################################
#Initialise variables
################################################
APPEND_DATE=0
EXE=${EXE_DEFAULT}
DIR=${PATH_DEF_PRE}
TARG_EXE=${TARG_EXE_DEF}
VARTOK=${VARTOKDEF}
INP_FILE_TARG=""
NOVARS=1
DIR_SPEC=0
OVERWRITE=-1
DRYRUN=0
MKSUBOPTS=-1
################################################
#Update variables based on options
################################################
while getopts “hae:E:d:i:I:tV:v:s:S:” OPTIONS
do
case $OPTIONS in
h) #Usage
usage
exit 1
;;
a) #Append date to directory?
APPEND_DATE=1
;;
e) #Executable
EXE=${OPTARG}
;;
d) #Directory name
DIR_SPEC=1
DIR=${OPTARG}
;;
i) #Input file name
INP_FILE=${OPTARG}
;;
I) #Input file name target
INP_FILE_TARG=${OPTARG}
;;
E) #Executable target name
TARG_EXE=${OPTARG}
;;
s) #Submission script
SUBFILE=${OPTARG}
;;
S) #Options to pass to mksub
MKSUBOPTS=${OPTARG}
;;
t) #Dryrun mode
DRYRUN=1
;;
V) #Variable token string
VARTOK=${OPTARG}
;;
v) #Variable values
NOVARS=0
VARVALS=${OPTARG}
;;
*)
usage
exit 1
;;
esac
done
################################################
#Post update check/set extra variables
################################################
#//Did we specify the directory?
if [ ${DIR_SPEC} -eq 0 ]
then
APPEND_DATE=1
fi
#//Did we specify an input file? If not try to find one
if [ "${INP_FILE}" == "" ]
then
#Search for matching files
INPLST=`ls *.in 2>/dev/null | wc -l`
#Handle cases
if [ ${INPLST} -eq 1 ] #Only one option
then
INP_FILE=`ls *.in`
elif [ ${INPLST} -eq 0 ] #No options
then
errmesg "No input file specified and none found."
exit 1
else #Several options
pickfile "*.in" INP_FILE
if [ "${INP_FILE}" == "" ]
then
errmesg "No input file selected."
exit 1
fi
fi
fi
#//Variables to array
if [ ${NOVARS} -eq 0 ]
then
VARS=(`echo $VARVALS | tr "," "\n"`)
NVAR=${#VARS[@]}
fi
#//Get input file base name
INP_BASE=`basename ${INP_FILE}`
#//Make sure executable path is absolute
rel2abs ${EXE} EXE_ABS
#//Did we set an input file target?
if [ "${INP_FILE_TARG}" == "" ]
then
INP_FILE_TARG=${INP_BASE}
else
INP_FILE_TARG=`basename ${INP_FILE_TARG}`
fi
#//Do we want to append date? If so do it
if [ ${APPEND_DATE} -eq 1 ]
then
#Get date/time string (day,month,year,hour,minute,second,nanosecond)
DTSTR=`date +"_%d_%m_%y_AT_%H_%M_%S_%N"`
#Update directory name
DIR=${DIR}${DTSTR}
fi
################################################
#Validation
################################################
#Directory exists already?
if [ -e ${DIR} ]
then
warnmesg "Directory '${DIR}' already exists."
yesno "Overwrite? " OVERWRITE
if [ ${OVERWRITE} -eq 0 ]
then
warnmesg "Not overwriting so quit."
exit 1
fi
fi
#Executable exists?
if [ ! -e ${EXE_ABS} ]
then
errmesg "Executable '${EXE_ABS}' does not exist."
exit 1
fi
#Input file exists?
if [ ! -e ${INP_FILE} ]
then
errmesg "Input file '${INP_FILE}' does not exist."
exit 1
fi
#Submission script
if [ ! "${SUBFILE}" == "" ]
then
if [ ! -e ${SUBSCRIPT} ]
then
errmesg "Submission script '${SUBFILE}' does not exist."
exit 1
fi
fi
################################################
#//Now make directory copy files etc.
################################################
if [ ${DRYRUN} -eq 0 ]
then
#Make directory
if [ ! ${OVERWRITE} -eq 1 ]
then
mkdir ${DIR}
fi
#Copy input file
cp ${INP_FILE} ${DIR}/${INP_FILE_TARG}
#Link executable
if [ ! ${OVERWRITE} -eq 1 ]
then
ln -s ${EXE_ABS} ${DIR}/${TARG_EXE}
else
ln -sf ${EXE_ABS} ${DIR}/${TARG_EXE}
fi
#Copy submission script
if [ ! "${SUBFILE}" == "" ]
then
cp ${SUBFILE} ${DIR}/
fi
else
echo -e "Directory :\t"${DIR}
echo -e "Input file :\t"${INP_FILE}
echo -e "Target :\t"${INP_FILE_TARG}
echo -e "Executable :\t"${EXE_ABS}
echo -e "Target :\t"${TARG_EXE}
if [ ! "${SUBFILE}" == "" ]
then
echo -e "Submission :\t"${SUBFILE}
fi
if [ ! "${MKSUBOPTS}" == "-1" ]
then
echo -e "Mksub opts :\t"${MKSUBOPTS}
fi
fi
################################################
#//Now onto the variable replacement part
################################################
if [ ${NOVARS} -eq 0 ]
then
if [ ! ${DRYRUN} -eq 1 ]
then
#Get current location
CUR=`pwd`
#Move into directory
cd ${DIR}
fi
#Find number variables to replace in input file
NFVAR=`grep -E "${VARTOK}[0-9]+" ${INP_FILE_TARG} | sed 's/.*\('"${VARTOK}"'[0-9]\+\).*/\1/g' | sort -u | wc -l`
#Check if this matches our expectations
if [ ${NFVAR} -gt ${NVAR} ]
then
errmesg "Too many tokens in ${INP_FILE} (${NFVAR}). Only passed ${NVAR} values."
#Remove directory if required
if [ ! ${DRYRUN} -eq 1 ]
then
if [ ! ${OVERWRITE} -eq 1 ]
then
rm -rf ${DIR}
fi
fi
exit 1
elif [ ${NFVAR} -lt ${NVAR} ]
then
warnmesg "More variables passed (${NVAR}) than tokens (${NFVAR})"
fi
#Now can loop over variables and do replacement
if [ ${DRYRUN} -eq 1 ]
then
echo -e "Token replacements:"
fi
for i in `seq 1 ${NFVAR}`
do
#Token string
VARSTR=${VARTOK}${i}
#Get variable value
VARVAL=${VARS[$((${i}-1))]}
#Replace string
if [ ! ${DRYRUN} -eq 1 ]
then
sed --in-place 's/\b'"${VARSTR}"'\b/'"${VARVAL}"'/g' ${INP_FILE_TARG}
else
echo -e "\tToken ${i} : ${VARSTR} \tValue : ${VARVAL}"
fi
done
#Move backwards
if [ ! ${DRYRUN} -eq 1 ]
then
cd ${CUR}
fi
fi
################################################
#//Now make submission script if requested
################################################
if [ ! ${DRYRUN} -eq 1 ]
then
if [ ! "${MKSUBOPTS}" == "-1" ]
then
#Get current location
CUR=`pwd`
#Move into directory
cd ${DIR}
#Make script
mksub ${MKSUBOPTS}
#Move backwards
cd ${CUR}
fi
fi
################################################
#//Finish off
################################################
exit 0