forked from sayan01/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempl
executable file
·36 lines (29 loc) · 995 Bytes
/
templ
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
#!/bin/bash
# create a new file from a template file.
# works using the file extension.
# looks for the template file with same extension in
# templates folder and copies it to the cwd with the passed name
# usage:
# tmpl tree.cpp
# tmpl reversal.cp.cpp
# tmpl sortQ.java
# this script will replace initc initcpp initjava etc into one script
if test "$#" -lt 1 ; then
echo "usage: $(basename "$0") filename.ext"
exit 1
fi
if test -z "$EDITOR" ; then EDITOR=vi; fi
extension=$(echo "$1" | cut -d '.' -f2-)
TMPDIR="${HOME}/docs/libraries/templates"
if ! test -r "${TMPDIR}/template.${extension}"; then
echo "$extension template not present in ${TMPDIR}"
echo -n "create it? y/N: "
read -r answer
if test "$answer" == 'y' ; then "$EDITOR" "${TMPDIR}/template.${extension}";
if ! test -r "${TMPDIR}/template.${extension}" ; then echo "file not saved" ; exit 1; fi
else exit 1; fi
fi
if ! test -e "${1}"; then
cp "${TMPDIR}/template.${extension}" "./${1}"
fi
"$EDITOR" "${1}"