-
Notifications
You must be signed in to change notification settings - Fork 0
/
new
executable file
·130 lines (112 loc) · 2.13 KB
/
new
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
#!/bin/ksh
PROG=$(basename "$0")
: "${NEW_DIR:="$HOME/.new"}"
USAGE="
Usage: $PROG [ -h ] [ -l ] [ -o output ] [ -t newtemp ] [ template ]
Create new files based off of a common template
-h This help message
-l List available templates
-t newtemp Create a new template named newtemp
-o output Output new file template to file output
instead of stdout
"
if [ ! -d "$NEW_DIR/." ]
then
echo "$PROG: Error: could not find new files" \
"directory ~/.new or NEW_DIR ($NEW_DIR)" >&2
exit 1
fi
output=
LIST_OPT=0
NEWTEMP_OPT=0
TEMP_OPT=0
OUT_OPT=0
SYNTAX="$PROG: option syntax error."
if [ "$#" = 0 ]
then
echo "$USAGE" >&2
exit 1
fi
while [ "$#" -gt 0 ]
do
OPT="$1"
case "$OPT" in
-h) echo "$USAGE" >&2
exit 1
;;
-l) LIST_OPT=1
;;
-t) NEWTEMP_OPT=1
newtemp="$2"
[ -z "$newtemp" ] && { echo "$SYNTAX" ;echo "$USAGE" ;exit 1 ;} >&2
shift
;;
-o) OUT_OPT=1
output="$2"
[ -z "$output" ] && { echo "$SYNTAX" ;echo "$USAGE" ;exit 1 ;} >&2
shift
;;
# unknown option
-?) echo "$SYNTAX" >&2
echo "$USAGE" >&2
exit 1
;;
*) break
;;
esac
shift
done
if [ "$#" != 0 ]
then
template="$1"
TEMP_OPT=1
shift
if [ "$#" -gt 0 ]
then
echo "$SYNTAX" >&2
echo "$USAGE" >&2
exit 1
fi
fi
if [ "$LIST_OPT" = 1 ]
then
echo "Templates:"
for t in "$NEW_DIR"/*
do
echo " $(basename "$t")"
done
exit 0
fi
if [ "$NEWTEMP_OPT" = 1 ]
then
if [ -f "$NEW_DIR/$newtemp" ]
then
echo "$PROG: Error: Template $newtemp already exists." >&2
exit 1
fi
touch "$NEW_DIR/$newtemp"
ED=
{ [ -n "$VISUAL" ] && ED="$VISUAL" ;} || \
{ [ -n "$EDITOR" ] && ED="$EDITOR" ;} || \
{ [ -e /usr/bin/vi ] && ED=/usr/bin/vi ;} || \
{ [ -e /bin/vi ] && ED=/bin/vi ;} || \
{ [ -e /bin/ed ] && ED=/bin/ed ;} || \
{ echo "$PROG: Could not located an editor. " \
"New template created at $NEW_DIR/${newtemp}." >&2 ;exit 1 ;}
"$ED" "$NEW_DIR/$newtemp"
exit 0
fi
if [ "$TEMP_OPT" = 1 ]
then
if [ ! -f "$NEW_DIR/$template" ]
then
echo "$PROG: Error: Template could not be found ($template)." >&2
exit 1
fi
if [ "$OUT_OPT" = 1 ]
then
eval "exec 1>$output"
fi
cat "$NEW_DIR/$template"
exit 0
fi